db.post.find()    {  _id  :&nbs..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

【MongoDB學習筆記14】MongoDB的查詢:find基礎

發(fā)布時間:2020-09-26 06:39:53 來源:網(wǎng)絡 閱讀:607 作者:StanlyCheng 欄目:MongoDB數(shù)據(jù)庫

MongoDB中使用find來進行查詢,返回一個集合中的文檔的子集;

返回文檔集合blog中的所有文檔:

> db.post.find()   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
>

 返回指定的文檔:

> db.post.find({"id":1})   
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
>

返回指定的鍵值:

> db.post.find({},{"id":1,"age":1})   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "age" : 30 }    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "age" : 30 }    
>

 默認總是會返回“_id”鍵,使用下面的方法可以不返回“ _id”鍵:

> db.post.find({},{"id":1,"age":1,"_id":0})   
{ "id" : 2, "age" : 30 }    
{ "id" : 1, "age" : 30 }    
>

   
可以將多個條件組合在一起,例如查詢名字為“Joe”且ID為1的文檔:

> db.post.find({"name":"joe"})   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
> db.post.find({"name":"joe","id":1})    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
>



向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI