04_MongoDB_查询文档

阅读更多

find文档

1.find简介

使用find查询集合中符合条件的子集合

db.test.blog.find();

 类似于sql查询

select * from test.blog

 上面的查询是返回多有多有集合,并且是所有键。有时我们也会指定返回部分键,这样方式可以减少IO

> db.test.blog.find({},{"age":1,"name":1});
{ "_id" : 1, "age" : 1, "name" : "joe" }
{ "_id" : ObjectId("533a1c27b653a97435a02030") }
{ "_id" : "2" }

 类似sql查询

select age , name from test.blog

 使用带条件的查询举例:

> db.test.blog.find({"age":1});
{ "_id" : 1, "age" : 1, "like" : [ "eat", "abc" ], "name" : "joe" }

 类似sql查询

select * from test.blog where age = 1

 

2.查询条件

比较操作符:

命令 描述
$lt <
$lte <=
$gt >
$gte >=

 

> db.student.find({age:{$lte:12,$gte:10}});

 类似sql

select * from student where age >= 10 and age <= 12

 $ne:不等,能用于多有类型的数据

> db.student.find({age:{$ne:10}});

 类似sql

select * from student where age != 10

 

3.OR查询

命令 描述
$in 查询一个键的多个值
$or 用来完成多个键值的任意给定值

 

> db.student.find({age:{$in:[10,13]}});
{ "_id" : 0, "name" : "aa0", "age" : 10 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }

 类比sql

select * from student where age in (10,13);

 

> db.student.find({"$or":[{"age":{$in:[10,13]}},{"name":"aa7"}]});
{ "_id" : 0, "name" : "aa0", "age" : 10 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }

 类比sql

select * from student where age in (10,13) or name = 'aa0'

 

4.$not

$not是原条件句,可以用在任何其他条件之上

> db.student.find({"age":{$not:{"$mod":[5,1]}}});
{ "_id" : 0, "name" : "aa0", "age" : 10 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }
{ "_id" : 9, "name" : "aa9", "age" : 14 }

 $mod:将查询的值除以第一个参数“5”,如果余数等于第二个参数“1”那么返回该值

 

5.null

null可以匹配自身(key对应的值为null),还可以匹配“不存在的”(文档中不存在这个key)。

> db.student.find({"like":null});
{ "_id" : 1, "name" : "aa1", "age" : 11 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 6, "name" : "aa6", "age" : 11 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }
{ "_id" : 9, "name" : "aa9", "age" : 14 }
{ "_id" : 0, "age" : 10, "like" : null, "name" : "aa0" }

> db.student.find({"like":null,"like":{$exists:true}});
{ "_id" : 0, "age" : 10, "like" : null, "name" : "aa0" }

 说明:$exists:判断键是否存在

 

6.正则表达式

> db.student.find({"name":/aa/});
{ "_id" : 1, "name" : "aa1", "age" : 11 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 6, "name" : "aa6", "age" : 11 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }
{ "_id" : 9, "name" : "aa9", "age" : 14 }
{ "_id" : 0, "age" : 10, "like" : null, "name" : "aa0" }
> db.student.find({"name":/aa1/});
{ "_id" : 1, "name" : "aa1", "age" : 11 }

 使用/reg/来表示正则

 

7.查询数组

命令 描述
$all 匹配数组中的多个元素
$size 匹配数组的长度
$slice 返回数组中的子集合

 

> db.food.find({"fruit":{$all:["apple","banana"]}});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }

> db.food.find({"fruit":"apple"});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }

 

> db.food.find({"fruit":{$size:3}});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }

 

8.查询内嵌文档

用“.”表示法是查询文档区别于其他文档的主要特点。查询文档可以包含点,来表示深入内嵌文档内部。点表示法也是待插入的文档不能包含"."的原因。

> db.food.insert({"_id":4,"fruit":{"apple":"good","banana":"good"}});
> db.food.insert({"_id":5,"fruit":{"apple":"best","banana":"good"}});
> db.food.find({"fruit.apple":"good"});
{ "_id" : 4, "fruit" : { "apple" : "good", "banana" : "good" } }
> db.food.find({"fruit.apple":"best"});
{ "_id" : 5, "fruit" : { "apple" : "best", "banana" : "good" } }

 

9.$where

如果前面的查询方法都不能实现,那么就轮到$where子句了,用它可以执行任意javascript作为查询的一部分。

你可能感兴趣的:(mongodb查询)