新聞中心

田家庵網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),田家庵網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為田家庵上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的田家庵做網(wǎng)站的公司定做!
find() 方法
想要查詢集合中的文檔,可以使用 MongoDB 中的 find() 方法,find() 方法可以將查詢結(jié)果以非結(jié)構(gòu)化的方式展示出來,其語法格式如下:
db.collection_name.find(query, projection)
語法說明如下:
- query:可選參數(shù),使用查詢操作符指定查詢條件;
- projection:可選參數(shù),使用投影操作符指定返回的鍵。查詢時若要返回文檔中所有鍵值,只需省略該參數(shù)即可(默認(rèn)省略)。
【示例】向集合中插入一些數(shù)據(jù)并使用 find() 方法查詢集合中的所有文檔:
> db.mycol.insert([
... {
... title: "MongoDB教程",
... description: "MongoDB 是一個 Nosql 數(shù)據(jù)庫",
... by: "編程幫",
... url: "http://www.biancheng.net",
... tags: ["mongodb", "database", "NoSQL"],
... likes: 999
... },
... {
... title: "NoSQL數(shù)據(jù)庫",
... description: "NoSQL數(shù)據(jù)庫中沒有數(shù)據(jù)表",
... 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 數(shù)據(jù)庫", "by" : "編程幫", "url" : "http://www.biancheng.net", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 999 }
{ "_id" : ObjectId("6031c02ae492ab9be9450303"), "title" : "NoSQL數(shù)據(jù)庫", "description" : "NoSQL數(shù)據(jù)庫中沒有數(shù)據(jù)表", "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() 方法查詢出的結(jié)果看起來比較凌亂,MongoDB 中還提供了一個 pretty() 方法來格式化查詢到的結(jié)果,讓查詢到的數(shù)據(jù)以更易讀的方式展示出來,具體語法如下:
db.collection_name.find(query, projection).pretty()
【示例】在使用 find() 查詢數(shù)據(jù)時,使用 pretty() 方法來格式化查詢到的數(shù)據(jù):
> db.mycol.find().pretty()
{
"_id" : ObjectId("6031c02ae492ab9be9450302"),
"title" : "MongoDB教程",
"description" : "MongoDB 是一個 Nosql 數(shù)據(jù)庫",
"by" : "編程幫",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
{
"_id" : ObjectId("6031c02ae492ab9be9450303"),
"title" : "NoSQL數(shù)據(jù)庫",
"description" : "NoSQL數(shù)據(jù)庫中沒有數(shù)據(jù)表",
"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 數(shù)據(jù)庫",
"by" : "編程幫",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
條件查詢
MongoDB 中也支持類似關(guān)系型數(shù)據(jù)庫中 where 字句的條件查詢,下表中列舉了 MongoDB 中條件查詢與關(guān)系型數(shù)據(jù)庫中 where 字句的比較:
| 操作 | 格式 | 范例 | 關(guān)系型數(shù)據(jù)庫中的類似語句 |
|---|---|---|---|
| 等于 | { |
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 |
| 數(shù)組中的值 | { |
db.col.find({title:{$in:["編程幫", "MongoDB教程"]}}) | where title in("編程幫", "MongoDB教程") |
| 不數(shù)組中的值 | { |
db.col.find({title:{$nin:["編程幫", "MongoDB教程"]}}) | where title not in("編程幫", "MongoDB教程") |
AND條件語句
MongoDB 的 find() 方法可以傳入多個鍵(key),每個鍵以逗號隔開,類似于常規(guī) 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 數(shù)據(jù)庫",
"by" : "編程幫",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
上面給出的示例等效于 SQL 語句中的“where by ='編程幫' AND title ='MongoDB教程'”。您可以在 find() 方法中傳遞任意數(shù)量的鍵/值對。另外,在使用 AND 條件語句時您也可以省略其中的 $and 關(guān)鍵字,如下所示:
> db.mycol.find({title:"MongoDB教程", by:"編程幫"}).pretty()
{
"_id" : ObjectId("6031c02ae492ab9be9450302"),
"title" : "MongoDB教程",
"description" : "MongoDB 是一個 Nosql 數(shù)據(jù)庫",
"by" : "編程幫",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
OR 條件語句
MongoDB 中的 OR 條件語句需要使用 $or 關(guān)鍵字,語法格式如下:
db.collection_name.find({$or:[{
【示例】在集合中查詢“title”=“MongoDB教程”或者“by”=“編程幫”的文檔:
> db.mycol.find({$or:[{title:"MongoDB教程"}, {by:"編程幫"}]}).pretty()
{
"_id" : ObjectId("6031c02ae492ab9be9450302"),
"title" : "MongoDB教程",
"description" : "MongoDB 是一個 Nosql 數(shù)據(jù)庫",
"by" : "編程幫",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}
{
"_id" : ObjectId("6031c02ae492ab9be9450303"),
"title" : "NoSQL數(shù)據(jù)庫",
"description" : "NoSQL數(shù)據(jù)庫中沒有數(shù)據(jù)表",
"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 聯(lián)合使用
下面通過同時使用 AND 和 OR 條件語句,來實現(xiàn) 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 數(shù)據(jù)庫",
"by" : "編程幫",
"url" : "http://www.biancheng.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 999
}文章題目:MongoDB查詢文檔
網(wǎng)頁地址:http://www.dlmjj.cn/article/cdphhio.html


咨詢
建站咨詢
