3.mongodb 查詢

MongoDB 查詢文檔使用 find() 方法。ide

find() 方法以非結構化的方式來顯示全部文檔。spa

語法格式以下:code

db.collection.find(query, projection)orm

query :可選,使用查詢操做符指定查詢條件教程

projection :可選,使用投影操做符指定返回的鍵。查詢時返回文檔中全部鍵值, 只需省略該參數便可(默認省略)。文檔


#在find後面加上pretty 方法後還能夠更容易閱讀的格式來顯示:it

 db.tmp.find()io

{ "_id" : ObjectId("5c1757c7f4e84940e0bd578b"), "name" : "aaa", "pwd" : "123", "birthday" : ISODate("1995-09-05T00:00:00Z") }table

{ "_id" : ObjectId("5c1757f5f4e84940e0bd578c"), "name" : "bbb", "pwd" : "321", "birthday" : ISODate("1999-10-10T00:00:00Z") }class

{ "_id" : ObjectId("5c177b84f4e84940e0bd578d"), "name" : "suhaozhi", "pwd" : "suhaozhi", "phone" : [ { "phone_num" : "13333333333", "type" : "cmcc" }, { "phone_num" : 15555555555, "type" : "chinanet" } ], "type" : "chinanet" }

{ "_id" : ObjectId("5c177ba1f4e84940e0bd578e"), "name" : "suhaozhi", "pwd" : "su", "phone" : [ { "phone_num" : "13333333332", "type" : "cmcc" }, { "phone_num" : 15655555555, "type" : "cnnet" } ] }

{ "_id" : ObjectId("5c178a62b1d327866c98c9f7"), "x" : "y" }

{ "_id" : ObjectId("5c1f226c87756129d06d2718"), "username" : "aaa", "phone_num" : "13333333333" }

{ "_id" : ObjectId("5c1f232587756129d06d2719"), "username" : "bbb", "phone_num" : [ "123", "456" ] }

mgset-6039947:PRIMARY> db.tmp.find().pretty()

{

"_id" : ObjectId("5c1757c7f4e84940e0bd578b"),

"name" : "aaa",

"pwd" : "123",

"birthday" : ISODate("1995-09-05T00:00:00Z")

}

{

"_id" : ObjectId("5c1757f5f4e84940e0bd578c"),

"name" : "bbb",

"pwd" : "321",

"birthday" : ISODate("1999-10-10T00:00:00Z")

}

{

"_id" : ObjectId("5c177b84f4e84940e0bd578d"),

"name" : "suhaozhi",

"pwd" : "suhaozhi",

"phone" : [

{

"phone_num" : "13333333333",

"type" : "cmcc"

},

{

"phone_num" : 15555555555,

"type" : "chinanet"

}

],

"type" : "chinanet"

}

{

"_id" : ObjectId("5c177ba1f4e84940e0bd578e"),

"name" : "suhaozhi",

"pwd" : "su",

"phone" : [

{

"phone_num" : "13333333332",

"type" : "cmcc"

},

{

"phone_num" : 15655555555,

"type" : "cnnet"

}

]

}

{ "_id" : ObjectId("5c178a62b1d327866c98c9f7"), "x" : "y" }

{

"_id" : ObjectId("5c1f226c87756129d06d2718"),

"username" : "aaa",

"phone_num" : "13333333333"

}

{

"_id" : ObjectId("5c1f232587756129d06d2719"),

"username" : "bbb",

"phone_num" : [

"123",

"456"

]

}


查詢條件:

若是你熟悉常規的 SQL 數據,經過下表能夠更好的理解 MongoDB 的條件語句查詢:


操做 格式 範例 RDBMS中的相似語句
等於 {<key>:<value>} db.col.find({"by":"菜鳥教程"}).pretty() where by = '菜鳥教程'
小於 {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}).pretty() where likes < 50
小於或等於 {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}).pretty() where likes <= 50
大於 {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}).pretty() where likes > 50
大於或等於 {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}).pretty() where likes >= 50
不等於 {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}}).pretty() where likes != 50


AND 條件查詢:

MongoDB find() 方法能夠傳入多個鍵(key),每一個鍵(key)以逗號隔開,即常規 SQL AND 條件。

例:

db.col.find({key1:value1, key2:value2}).pretty()


or 條件查詢:

MongoDB OR 條件語句使用了關鍵字 $or,語法格式以下:

>db.col.find(

   {

      $or: [

         {key1: value1}, {key2:value2}

      ]

   }

).pretty()



and與or聯合使用:

如下實例演示了 AND OR 聯合使用,相似常規 SQL 語句爲: 'where age<40 AND (name = 'ayumi' OR jobs = 'singer')


db.col.find({"age": {$lt:40}, $or: [{"name": "ayumi"},{"jobs": singer"}]}).pretty()



對內嵌文檔進行操做:

假如說咱們這個集合裏面的文檔都是這種格式:

{ "_id" : ObjectId("5c177b84f4e84940e0bd578d"), "name" : "suhaozhi", "pwd" : "suhaozhi", "phone" : [ { "phone_num" : "13333333333", "type" : "cmcc" }, { "phone_num" : 15555555555, "type" : "chinanet" } ], "type" : "chinanet" }

如今須要搜索出phone字段中type子字段的值爲CMCC的記錄。

db.tmp.find({phone:{$elemMatch:{cs:"cmcc"}}});

相關文章
相關標籤/搜索