MongoDB基础教程系列--第四篇 MongoDB 查询文档
查询文档
查询文档可以用 find() 方法查询全部文档,可以用 findOne() 查询第一个文档,当然还可以根据 条件操作符 和 $type操作符 查询满足条件的文档。
1、find() 和 findOne()
MongoDB 用 find() 查询指定集合的全部文档
格式
1 | db.COLLECTION_NAME.find() |
范例
1 2 3 4 5 6 | > db. user .find() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754d" ), "name" : "user1" , "age" : 19, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754e" ), "name" : "user2" , "age" : 20, "sex" : "woman" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754f" ), "name" : "user3" , "age" : 19, "sex" : "woman" } > |
如果想要格式化显示查询结果,我们需要用 pretty() 方法
格式如下
1 | db.COLLECTION_NAME.find().pretty() |
再次显示查询结果如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | > db. user .find().pretty() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754d" ), "name" : "user1" , "age" : 19, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754e" ), "name" : "user2" , "age" : 20, "sex" : "woman" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754f" ), "name" : "user3" , "age" : 19, "sex" : "woman" } > |
除了 find() 方法,还有一个 findOne() 方法,它只会返回一个文档
1 2 3 4 5 6 7 | > db. user .findOne() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } |
2、条件操作符
如果你熟悉sql,那么下表可以更好的理解 MongoDB 条件查询
操作 | 格式 | 范例 | RDBMS中的类似语句 |
---|
等于 | {<key>:<value> } | db.user.find({"name":"liruihuan"}).pretty() | where name = 'liruihuan' |
小于 | {<key>:{$lt:<value>}} | db.user.find({"age":{$lt:18}}).pretty() | where age < 18 |
小于或等于 | {<key>:{$lte:<value>}} | db.user.find({"age":{$lte:18}}).pretty() | where age <= 18 |
大于 | {<key>:{$gt:<value>}} | db.user.find({"age":{$gt:18}}).pretty() | where age > 18 |
大于或等于 | {<key>:{$gte:<value>}} | db.user.find({"age":{$gte:18}}).pretty() | where age >= 18 |
不等于 | {<key>:{$ne:<value>}} | db.user.find({"age":{$ne:18}}).pretty() | where age != 18 |
范例
1、查询 user 集合中 name = "liruihuan" 的文档,代码如下
1 2 3 4 5 6 7 8 | > db. user .find({ "name" : "liruihuan" }).pretty() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } > |
2、查询 user 集合中 age > 19 的文档,代码如下
1 2 3 4 5 6 7 8 | > db. user .find({ "age" :{$gt:19}}).pretty() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754e" ), "name" : "user2" , "age" : 20, "sex" : "woman" } > |
为了更好的理解条件操作符,可以用英文解释一下
1 2 3 4 5 6 7 8 9 | $gt -- greater than $gte -- gt equal $lt -- less than $lte -- lt equal $ne -- not equal |
MongoDB 中的 and 条件
MongoDB 的 find() 方法可以传入多个键(key),每个键(key)以逗号隔开,MongoDB 会把这些键作为 and 条件,及常规 SQL 的 AND 条件。
格式
1 | db.collection.find({key1:value1, key2:value2}).pretty() |
范例
查询 user 集合中 name 值为 liruihuan 且 age 值为 18 的文档
1 2 3 4 5 6 7 | > db. user .find({ "name" : "liruihuan" , "age" :18}).pretty() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } |
此实例类似于 sql 中 where 条件
1 | WHERE name = 'liruihuan' AND age=18 |
MongoDB 中的 or 条件
MongoDB 中 or 条件用 $or关键字
格式
1 2 3 4 5 6 7 | db.collection.find( { $ or : [ {key1: value1}, {key2:value2} ] } ).pretty() |
范例
查询 user 集合中 name 值为 liruihuan 或 name 值为 user1 的文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | > db. user .find({$ or :[{ "name" : "liruihuan" },{ "name" : "user1" }]}).pretty() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754d" ), "name" : "user1" , "age" : 19, "sex" : "man" } > |
此实例类似于 sql 中 where 条件
1 | WHERE name = 'liruihuan' OR name = "user1" |
MongoDB 中 and 和 or 结合使用
范例
查询 user 集合中 age 值大于17 且 (name 值为 liruihuan 或 name 值为 user1) 的文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | > db. user .find({ "age" :{$gt:17},$ or :[{ "name" : "liruihuan" },{ "name" : "user1" }]}).pretty() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754d" ), "name" : "user1" , "age" : 19, "sex" : "man" } > |
此实例类似于 sql 中 where 条件
1 | WHERE age > 17 ( name = 'liruihuan' OR name = "user1" ) |
注:and 条件是在大括号中,or 条件是在中括号里的
3、$type 操作符
基于BSON类型来查询集合中匹配的数据类型,并返回结果。
下表列举了可使用的数据类型
类型 | 数字 | 备注 |
---|
Double | 1 | |
String | 2 | |
Object | 3 | |
Array | 4 | |
Binary data | 5 | |
Undefined | 6 | 已废弃。 |
Object id | 7 | |
Boolean | 8 | |
Date | 9 | |
Null | 10 | |
Regular Expression | 11 | |
JavaScript | 13 | |
Symbol | 14 | |
JavaScript (with scope) | 15 | |
32-bit integer | 16 | |
Timestamp | 17 | |
64-bit integer | 18 | |
Min key | 255 | Query with -1. |
Max key | 127 | |
范例
查询 user 集合中 name 为 String 类型的文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | > db. user .find({ "name" :{$type:2}}).pretty() { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754b" ), "name" : "liruihuan" , "age" : 18, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754d" ), "name" : "user1" , "age" : 19, "sex" : "man" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754e" ), "name" : "user2" , "age" : 20, "sex" : "woman" } { "_id" : ObjectId( "58e1d2f0bb1bbc3245fa754f" ), "name" : "user3" , "age" : 19, "sex" : "woman" } > |