
田家庵网站建设公司成都创新互联,田家庵网站设计制作,有大型网站制作公司丰富经验。已为田家庵上1000+提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的田家庵做网站的公司定做!
 想要查询集合中的文档,可以使用 MongoDB 中的 find() 方法,find() 方法可以将查询结果以非结构化的方式展示出来,其语法格式如下:
 
db.collection_name.find(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 } ] }
 仅使用 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
                }
        ]
}
 除了可以使用 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 字句的比较:
| 操作 | 格式 | 范例 | 关系型数据库中的类似语句 | 
|---|---|---|---|
| 等于 | { | db.col.find({"by":"编程帮"}) | where by = '编程帮' | 
| 小于 | { | db.col.find({"likes":{$lt:50}}) | where likes < 50 | 
| 小于或等于 | { | db.col.find({"likes":{$lte:50}}) | where likes <= 50 | 
| 大于 | { | 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教程") | 
 MongoDB 的 find() 方法可以传入多个键(key),每个键以逗号隔开,类似于常规 SQL 语句中的 AND 条件语句。语法格式如下:
 
 db.collection_name.find({$and:[{
 【示例】在集合中查询“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
}
 MongoDB 中的 OR 条件语句需要使用 $or 关键字,语法格式如下:
 
 db.collection_name.find({$or:[{
 【示例】在集合中查询“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 条件语句,来实现 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
}