在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)庫/ 查詢文檔
更新文檔
GridFS
Rockmongo 管理工具
Map Reduce
Java
創(chuàng)建備份
數(shù)據(jù)模型
創(chuàng)建數(shù)據(jù)庫
映射
查詢文檔
索引限制
ObjectId
刪除文檔
數(shù)據(jù)類型
高級索引
索引
優(yōu)勢
記錄排序
查詢分析
插入文檔
刪除集合
全文檢索
創(chuàng)建集合
概述
數(shù)據(jù)庫引用
覆蓋索引查詢
安裝環(huán)境
PHP
刪除數(shù)據(jù)庫
固定集合
關系
聚合
自動增長
復制
限制記錄
部署
分片
正則表達式
原子操作

查詢文檔

find() 方法

要想查詢 MongoDB 集合中的數(shù)據(jù),使用 find() 方法。

語法格式

find() 方法的基本格式為:

>db.COLLECTION_NAME.find()

find() 方法會以非結構化的方式來顯示所有文檔。

pretty() 方法

用格式化方式顯示結果,使用的是 pretty() 方法。

語法格式

>db.mycol.find().pretty()

范例

>db.mycol.find().pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

除了 find() 方法之外,還有一個 findOne() 方法,它只返回一個文檔。

MongoDB 中類似于 WHERE 子句的語句

如果想要基于一些條件來查詢文檔,可以使用下列操作。

操作 格式 范例 RDBMS中的類似語句
等于 {<key>:<value>} db.mycol.find({"by":"tutorials point"}).pretty() where by = 'tutorials point'
小于 {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
小于或等于 {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
大于 {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
大于或等于 {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
不等于 {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50

MongoDB 中的 And 條件

語法格式

find() 方法中,如果傳入多個鍵,并用逗號(,)分隔它們,那么 MongoDB 會把它看成是 AND 條件。AND 條件的基本語法格式為:

>db.mycol.find({key1:value1, key2:value2}).pretty()

范例

下例將展示所有由 “tutorials point” 發(fā)表的標題為 “MongoDB Overview” 的教程。

>db.mycol.find({"by":"tutorials point","title": "MongoDB Overview"}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

對于上例這種情況,RDBMS 采用的 WHERE 子句將會是:where by='tutorials point' AND title='MongoDB Overview'。你可以在 find 子句中傳入任意的鍵值對。

MongoDB 中的 OR 條件

語法格式

若基于 OR 條件來查詢文檔,可以使用關鍵字 $or。 OR 條件的基本語法格式為:

>db.mycol.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

范例

下例將展示所有由 “tutorials point” 發(fā)表的標題為 “MongoDB Overview” 的教程。

>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

結合使用 AND 與 OR 條件

范例

下例所展示文檔的條件為:喜歡數(shù)大于 100,標題是 “MongoDB Overview”,或者是由 “tutorials point” 所發(fā)表的。響應的 SQL WHERE 子句為:where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')

>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>
上一篇:GridFS下一篇:Map Reduce