365Tools
发布时间:2024-03-29 11:30:01
前面我们介绍了怎么将文档插入到集合中,本节我们来介绍一下如何从集合中查询指定的文档。db.collection_name.find(query, projection)
语法说明如下:
> db.mycol.insert([
... {
... title: "MongoDB教程",
... description: "MongoDB 是一个 Nosql 数据库",
... by: "编程帮",
... url: "http://www.www.365tools.cn",
... tags: ["mongodb", "database", "NoSQL"],
... likes: 999
... },
... {
... title: "NoSQL数据库",
... description: "NoSQL数据库中没有数据表",
... by: "编程帮",
... url: "http://www.www.365tools.cn",
... 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.www.365tools.cn", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 }
{ "_id" : ObjectId("6031c02ae492ab9be9450303"), "title" : "NoSQL数据库", "description" : "NoSQL数据库中没有数据表", "by" : "编程帮", "url" : "http://www.www.365tools.cn", "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.www.365tools.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
{
"_id" : ObjectId("6031c02ae492ab9be9450303"),
"title" : "NoSQL数据库",
"description" : "NoSQL数据库中没有数据表",
"by" : "编程帮",
"url" : "http://www.www.365tools.cn",
"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.www.365tools.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
| 操作 | 格式 | 范例 | 关系型数据库中的类似语句 |
|---|---|---|---|
| 等于 |
{ |
db.col.find({"by":"编程帮"}) | where by = '编程帮' |
| 小于 |
{ |
db.col.find({"likes":{$lt:50}}) | where likes |
| 小于或等于 |
{ |
db.col.find({"likes":{$lte:50}}) | where likes |
| 大于 |
{ |
db.col.find({"likes":{$gt:50}}) | where likes > 50 |
| 大于或等于 |
{ |
db.col.find({"likes":{$gte:50}}) | where likes >= 50 |
| 不等于 |
{ |
db.col.find({"likes":{$ne:50}}) | where likes != 50 |
| 数组中的值 |
{ |
db.col.find({title:{$in:["编程帮", "MongoDB教程"]}}) | where title in("编程帮", "MongoDB教程") |
| 不数组中的值 |
{ |
db.col.find({title:{$nin:["编程帮", "MongoDB教程"]}}) | where title not in("编程帮", "MongoDB教程") |
db.collection_name.find({$and:[{
> db.mycol.find({$and:[{title:"MongoDB教程"}, {by:"编程帮"}]}).pretty()
{
"_id" : ObjectId("6031c02ae492ab9be9450302"),
"title" : "MongoDB教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "编程帮",
"url" : "http://www.www.365tools.cn",
"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.www.365tools.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
db.collection_name.find({$or:[{
> db.mycol.find({$or:[{title:"MongoDB教程"}, {by:"编程帮"}]}).pretty()
{
"_id" : ObjectId("6031c02ae492ab9be9450302"),
"title" : "MongoDB教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "编程帮",
"url" : "http://www.www.365tools.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
{
"_id" : ObjectId("6031c02ae492ab9be9450303"),
"title" : "NoSQL数据库",
"description" : "NoSQL数据库中没有数据表",
"by" : "编程帮",
"url" : "http://www.www.365tools.cn",
"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.www.365tools.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}