db.collection_name.find(query, projection)
语法说明如下:> db.mycol.insert([ ... { ... title: "MongoDB教程", ... description: "MongoDB 是一个 Nosql 数据库", ... by: "编程帮", ... url: "http://www.lmcjl.com", ... tags: ["mongodb", "database", "NoSQL"], ... likes: 999 ... }, ... { ... title: "NoSQL数据库", ... description: "NoSQL数据库中没有数据表", ... by: "编程帮", ... url: "http://www.lmcjl.com", ... tags: ["mongodb", "database", "NoSQL"], ... likes: 100, ... comments: [ ... { ... user:"admin", ... message: "第一个评论", ... dateCreated: new Date(2021,01,10,2,35), ... like: 0 ... } ... ] ... } ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.mycol.find() { "_id" : ObjectId("6031c02ae492ab9be9450302"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 } { "_id" : ObjectId("6031c02ae492ab9be9450303"), "title" : "NoSQL数据库", "description" : "NoSQL数据库中没有数据表", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100, "comments" : [ { "user" : "admin", "message" : "第一个评论", "dateCreated" : ISODate("2021-02-09T18:35:00Z"), "like" : 0 } ] }
db.collection_name.find(query, projection).pretty()
【示例】在使用 find() 查询数据时,使用 pretty() 方法来格式化查询到的数据:> db.mycol.find().pretty() { "_id" : ObjectId("6031c02ae492ab9be9450302"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 } { "_id" : ObjectId("6031c02ae492ab9be9450303"), "title" : "NoSQL数据库", "description" : "NoSQL数据库中没有数据表", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100, "comments" : [ { "user" : "admin", "message" : "第一个评论", "dateCreated" : ISODate("2021-02-09T18:35:00Z"), "like" : 0 } ] }
db.collection_name.findOne(query, projection)
【示例】使用 findOne() 方法从集合中查询“title”=“MongoDB教程”的文档:> db.mycol.findOne({title:"MongoDB教程"}) { "_id" : ObjectId("6031c02ae492ab9be9450302"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 }
操作 | 格式 | 范例 | 关系型数据库中的类似语句 |
---|---|---|---|
等于 | {<key>:<value>} | db.col.find({"by":"编程帮"}) | where by = '编程帮' |
小于 | {<key>:{$lt:<value>}} | db.col.find({"likes":{$lt:50}}) | where likes < 50 |
小于或等于 | {<key>:{$lte:<value>}} | db.col.find({"likes":{$lte:50}}) | where likes <= 50 |
大于 | {<key>:{$gt:<value>}} | db.col.find({"likes":{$gt:50}}) | where likes > 50 |
大于或等于 | {<key>:{$gte:<value>}} | db.col.find({"likes":{$gte:50}}) | where likes >= 50 |
不等于 | {<key>:{$ne:<value>}} | db.col.find({"likes":{$ne:50}}) | where likes != 50 |
数组中的值 | {<key>:{$in:[<value1>, <value2>, ...<valueN>]}} | db.col.find({title:{$in:["编程帮", "MongoDB教程"]}}) | where title in("编程帮", "MongoDB教程") |
不数组中的值 | {<key>:{$nin:[<value1>, <value2>, ...<valueN>]}} | db.col.find({title:{$nin:["编程帮", "MongoDB教程"]}}) | where title not in("编程帮", "MongoDB教程") |
db.collection_name.find({$and:[{<key1>:<value1>}, {<key2>:<value2>}], ...})
【示例】在集合中查询“title”=“MongoDB教程”同时“by”=“编程帮”的文档:> db.mycol.find({$and:[{title:"MongoDB教程"}, {by:"编程帮"}]}).pretty() { "_id" : ObjectId("6031c02ae492ab9be9450302"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 }上面给出的示例等效于 SQL 语句中的“where by ='编程帮' AND title ='MongoDB教程'”。您可以在 find() 方法中传递任意数量的键/值对。另外,在使用 AND 条件语句时您也可以省略其中的 $and 关键字,如下所示:
> db.mycol.find({title:"MongoDB教程", by:"编程帮"}).pretty() { "_id" : ObjectId("6031c02ae492ab9be9450302"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 }
db.collection_name.find({$or:[{<key1>: <value1>}, {<key2>:<value2>}]})
【示例】在集合中查询“title”=“MongoDB教程”或者“by”=“编程帮”的文档:> db.mycol.find({$or:[{title:"MongoDB教程"}, {by:"编程帮"}]}).pretty() { "_id" : ObjectId("6031c02ae492ab9be9450302"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 } { "_id" : ObjectId("6031c02ae492ab9be9450303"), "title" : "NoSQL数据库", "description" : "NoSQL数据库中没有数据表", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100, "comments" : [ { "user" : "admin", "message" : "第一个评论", "dateCreated" : ISODate("2021-02-09T18:35:00Z"), "like" : 0 } ] }
> db.mycol.find({"likes": {$gt:100}, $or: [{"by": "编程帮"},{"title": "MongoDB教程"}]}).pretty() { "_id" : ObjectId("6031c02ae492ab9be9450302"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "编程帮", "url" : "http://www.lmcjl.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 }
本文链接:http://task.lmcjl.com/news/17649.html