1、查询集合中的所有记录 |
|
db.users.find() |
{ "_id" : ObjectId( "528b1173613e3289197a6486" ), "name" : "lihuai" , "age" : 35, "status" : "A" , "groups" : [ "news" , "sports" ] } |
{ "_id" : ObjectId( "528b17ffef83cba2aee5bca9" ), "name" : "yekai" , "age" : 30, "status" : "A" , "groups" : [ "history" , "Math" ] } |
{ "_id" : ObjectId( "528b187eef83cba2aee5bcaa" ), "name" : "lixunhuan" , "age" : 40, "status" : "B" , "groups" : [ "music" , "Math" ] } |
2、查询集合中的前两条记录 |
|
db.users.find().limit(2) |
3、查询age==30的数据 |
|
db.users.find({age:30}) |
4、查询age>35的数据 |
|
db. user .find({age:{$gt:35}}) |
5、查询age>=35的数据 |
|
db. user .find({age:{$gte:35}}) |
6、<使用$lt,<=使用$lte,!=使用$ne |
|
7、 in 的使用 |
|
db.users.find({status:{$ in :[ "A" , "B" ]}}) |
8、 not in 使用$nin |
|
db.users.find({status:{$nin:[ "A" ]}}) |
9、查出status= "A" ,而且age>30的数据 |
|
db. user .find({status: "A" ,age:{$gt:30}}) |
10、查出status= "B" 或者age=30的数据 |
|
db.users.find( |
{$ or :[ |
{status: "B" }, |
{age:30} |
] |
} |
) |
11、查询 name 以 "ye" 开头的数据 |
|
db.users.find({ name :/^ye/}) |
12、查询 name 以 "an" 结尾的数据 |
|
db.users.find({ name :/an$/}) |
13、 where 的使用,查询出 name == "yekai" 或者age==35的数据 |
|
db.users.find( |
{$ where : function (){ |
return this. name == "yekai" || this.age==35 |
} |
} |
) |