MongoDB查询文档
前面我们介绍了怎么将文档插入到集合中,本节我们来介绍一下如何从集合中查询指定的文档。
【示例】向集合中插入一些数据并使用 find() 方法查询集合中的所有文档:
find() 方法
想要查询集合中的文档,可以使用 MongoDB 中的 find() 方法,find() 方法可以将查询结果以非结构化的方式展示出来,其语法格式如下:db.collection_name.find(query, projection)
语法说明如下:- query:可选参数,使用查询操作符指定查询条件;
- projection:可选参数,使用投影操作符指定返回的键。查询时若要返回文档中所有键值,只需省略该参数即可(默认省略)。
【示例】向集合中插入一些数据并使用 find() 方法查询集合中的所有文档:
> db.mycol.insert([
... {
... title: "MongoDB教程",
... description: "MongoDB 是一个 Nosql 数据库",
... by: "编程帮",
... url: "http://www.biancheng.net",
... tags: ["mongodb", "database", "NoSQL"],
... likes: 999
... },
... {
... title: "NoSQL数据库",
... description: "NoSQL数据库中没有数据表",
... by: "编程帮",
... url: "http://www.biancheng.net",
... 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.biancheng.net", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 }
{ "_id" : ObjectId("6031c02ae492ab9be9450303"), "title" : "NoSQL数据库", "description" : "NoSQL数据库中没有数据表", "by" : "编程帮", "url" : "http://www.biancheng.net", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100, "comments" : [ { "user" : "admin", "message" : "第一个评论", "dateCreated" : ISODate("2021-02-09T18:35:00Z"), "like" : 0 } ] }
pretty() 方法
仅使用 find() 方法查询出的结果看起来比较凌乱,MongoDB 中还提供了一个 pretty() 方法来格式化查询到的结果,让查询到的数据以更易读的方式展示出来,具体语法如下: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.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
{
"_id" : ObjectId("6031c02ae492ab9be9450303"),
"title" : "NoSQL数据库",
"description" : "NoSQL数据库中没有数据表",
"by" : "编程帮",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100,
"comments" : [
{
"user" : "admin",
"message" : "第一个评论",
"dateCreated" : ISODate("2021-02-09T18:35:00Z"),
"like" : 0
}
]
}
findOne() 方法
除了可以使用 find() 方法外,还可以使用 findOne() 方法查询集合中的文档,但 findOne() 方法仅能返回一个查询到的文档,其语法格式如下: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.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
条件查询
MongoDB 中也支持类似关系型数据库中 where 字句的条件查询,下表中列举了 MongoDB 中条件查询与关系型数据库中 where 字句的比较:| 操作 | 格式 | 范例 | 关系型数据库中的类似语句 |
|---|---|---|---|
| 等于 | {<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教程") |
AND条件语句
MongoDB 的 find() 方法可以传入多个键(key),每个键以逗号隔开,类似于常规 SQL 语句中的 AND 条件语句。语法格式如下: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.biancheng.net",
"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.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
OR 条件语句
MongoDB 中的 OR 条件语句需要使用 $or 关键字,语法格式如下: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.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
{
"_id" : ObjectId("6031c02ae492ab9be9450303"),
"title" : "NoSQL数据库",
"description" : "NoSQL数据库中没有数据表",
"by" : "编程帮",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100,
"comments" : [
{
"user" : "admin",
"message" : "第一个评论",
"dateCreated" : ISODate("2021-02-09T18:35:00Z"),
"like" : 0
}
]
}
AND 和 OR 联合使用
下面通过同时使用 AND 和 OR 条件语句,来实现 SQL 语句中的“where likes > 100 AND (by = '编程帮' OR title = 'MongoDB教程')”。
> db.mycol.find({"likes": {$gt:100}, $or: [{"by": "编程帮"},{"title": "MongoDB教程"}]}).pretty()
{
"_id" : ObjectId("6031c02ae492ab9be9450302"),
"title" : "MongoDB教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "编程帮",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
ICP备案:
公安联网备案: