MongoDB基础教程系列--第四篇 MongoDB 查询文档

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类型来查询集合中匹配的数据类型,并返回结果。

下表列举了可使用的数据类型

类型数字备注
Double1 
String2 
Object3 
Array4 
Binary data5 
Undefined6已废弃。
Object id7 
Boolean8 
Date9 
Null10 
Regular Expression11 
JavaScript13 
Symbol14 
JavaScript (with scope)15 
32-bit integer16 
Timestamp17 
64-bit integer18 
Min key255Query with -1.
Max key127 

范例

查询 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"

}

>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值