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"}}});