mongoDB中使用find来进行查询,返回一个集合中的文档的子集;返回文档集合blog中的所有文档:> db.post.find() { "_
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" }
>
--结束END--
本文标题: 【MongoDB学习笔记14】MongoDB的查询:find基础
本文链接: https://lsjlt.com/news/51728.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0