您好,登錄后才能下訂單哦!
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" } >
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。