3.mongodb 查詢
MongoDB 查詢文件使用 find() 方法。
find() 方法以非結構化的方式來顯示所有文件。
語法格式如下:
db.collection.find(query, projection)
query :可選,使用查詢操作符指定查詢條件
projection :可選,使用投影操作符指定返回的鍵。查詢時返回文件中所有鍵值, 只需省略該引數即可(預設省略)。
#在find後面加上pretty 方法後還可以更容易閱讀的格式來顯示:
db.tmp.find()
{ "_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" ] }
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"}}});