Mongodb查詢文件

2021-06-26 06:41:08 字數 2607 閱讀 6315

mongodb查詢文件

> db.blog.find()     --

檢視集合下的所有文件

db.blog.find({}).limit(50);

可以限制查詢文件的數量

> db.blog.find({}).limit(1);

> db.blog.find().pretty();  ---

格式化查詢文件加上

pretty()

"_id" : objectid("5438dd3fa7ccb1d4ecc7571d"),

"title" : "my blog post",

"content" : "here is blog past",

"date" : isodate("2014-10-11t07:32:55.652z")

"_id" : objectid("5438df80a7ccb1d4ecc7571e"),

"name" : "blog",

"author" : "wangwei"

> db.blog.findone()  ----

檢視集合下乙個文件

"_id" : objectid("5438dd3fa7ccb1d4ecc7571d"),

"title" : "my blog post",

"content" : "here is blog past",

"date" : isodate("2014-10-11t07:32:55.652z")

> db.blog.find().limit(50).pretty()  --

按條件查詢文件

"_id" : objectid("5438df80a7ccb1d4ecc7571e"),

"name" : "blog",

"author" : "wangwei"

對於上面給出的例子相當於

where

子句where name= 『blog』

mongodb中的and

> db.blog.find().limit(50);

> db.blog.find().limit(50);

> --

返回為空

對於上面給出的例子相當於

where

子句where name= 『blog』 and author =』wangwei』

mongodb中的or

> db.blog.find(, ] }).

limit(50).pretty()

"_id" : objectid("5438dd3fa7ccb1d4ecc7571d"),

"title" : "my blog post",

"content" : "here is blog past",

"date" : isodate("2014-10-11t07:32:55.652z")

"_id" : objectid("5438df80a7ccb1d4ecc7571e"),

"name" : "blog",

"author" : "wangwei"

對於上面給出的例子相當於

where

子句where name= 『blog』 or title =』 myblog post』

> db.blog.find(, ).limit(50).pretty();  --

查詢顯示特定列(字段)

對於上面給出的例子相當於

select name from

> db.blog.find(, ).limit(50).sort();  --

按欄位排序查詢

sort() 

方法接受乙個文件,其中包含的字段列表連同他們的排序順序。要指定排序順序1和

-1。1用於公升序排列,而

-1用於降序。

rdbms where

子句和mongodb

等同語句

要查詢檔案的一些條件的基礎上,可以使用下面的操作

www.yiibai.com

操作

語法

例子

rdbms 等同

equality

db.mycol.find().pretty()

where by = 'tutorials point'

less than

}db.mycol.find(}).pretty()

where likes < 50

less than equals

}db.mycol.find(}).pretty()

where likes <= 50

greater than

}db.mycol.find(}).pretty()

where likes > 50

greater than equals

}db.mycol.find(}).pretty()

where likes >= 50

not equals

}db.mycol.find(}).pretty()

where likes != 50

MongoDB 查詢文件

mongodb 查詢文件使用 find 方法。find 方法以非結構化的方式來顯示所有文件。mongodb 查詢資料的語法格式如下 db collection find query projection collection find query projection 如果你需要以易讀的方式來讀取資...

MongoDB 查詢文件

mongodb 查詢資料 db.collection.find query,projection 易讀的方式來讀取資料,可以使用 pretty 方法 db.col.find pretty 通過 by 和 title 鍵來查詢 db.col.find pretty 類似於 where 語句 where...

mongoDB 文件查詢

在關係型資料庫中,可以實現基於表上各種各樣的查詢,以及通過投影來返回指定的列。對於nosql mongodb而言,所有能夠在單錶上完成的查詢,在mongodb中也可以完全勝任。除此之外,由於mongodb支援基於文件巢狀以及陣列,因此mongodb也可以實現基於巢狀文件和陣列的查詢。具體見下文描述。...