返回所有的键
|
find()
|
> db.users.find() { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com", "joe@hotmail.com", "joe@php.net", "joe@python.org" ], "username" : "joe" } { "_id" : ObjectId("5518f4711d802f6252d90e81"), "username" : "bob", "emails" : [ "bob@example.com", "bob@gmail.com" ] } |
返回指定的键 |
find(
{}, {"username":1, "emails":1})
|
> db.users.find({}, {"username":1})
{ "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "username" : "joe" }
{ "_id" : ObjectId("5518f4711d802f6252d90e81"), "username" : "bob" }
|
返回不希望含有指定的键 |
find(
{}
, {"username":0})
|
> db.users.find({}, {"username":0}) { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com", "joe@hotmail.com", "joe@php.net", "joe@python.org" ] } { "_id" : ObjectId("5518f4711d802f6252d90e81"), "emails" : [ "bob@example.com", "bob@gmail.com" ] } |
条件查询
|
|
|
大于等于: $gte |
|
db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})
|
大于$gt |
|
|
小于$lt |
|
> start = new Date("01/01/2007") > db.users.find({"registered" : {"$lt" : start}}) |
小于等于$lte
|
|
|
不等于$ne |
db.users.find({"username" : {"$ne" : "joe"}}) | > db.users.find({username:{$ne:"joe"}}) { "_id" : ObjectId("5518f4711d802f6252d90e81"), "username" : "bob", "emails" : [ "bob@example.com", "bob@gmail.com" ] } |
AND查询,即多个条件的组合
|
> db.users.find({"username" : "joe", "age" : 27})
|
|
|
|
|
OR查询
|
|
|
$in
| 查询一个键的多个值, |
> db.users.find({username:{$in:["joe", "bob"]}}) { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com", "joe@hotmail.com", "joe@php.net", "joe@python.org" ], "username" : "joe" } { "_id" : ObjectId("5518f4711d802f6252d90e81"), "username" : "bob", "emails" : [ "bob@example.com", "bob@gmail.com" ] } |
|
$in可以指定不同类型的数据
|
> db.users.find({"user_id" : {"$in" : [12345, "joe"]}) |
$nin
| 与$in相对 |
> db.users.find({username:{$nin:["bob"]}}) { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com", "joe@hotmail.com", "joe@php.net", "joe@python.org" ], "username" : "joe" } |
$or |
|
> db.users.find() { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com", "joe@hotmail.com", "joe@php.net", "joe@python.org" ], "username" : "joe" } { "_id" : ObjectId("5518f4711d802f6252d90e81"), "username" : "bob", "emails" : [ "bob@example.com", "bob@gmail.com" ] } { "_id" : ObjectId("551907fc1d802f6252d90e86"), "username" : "jim", "emails" : [ "jim@gmail.com", "jim@hotmail.com" ] } > db.users.find({$or:[{username:"joe"},{username: "bob"}]}) { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com", "joe@hotmail.com", "joe@php.net", "joe@python.org" ], "username" : "joe" } { "_id" : ObjectId("5518f4711d802f6252d90e81"), "username" : "bob", "emails" : [ "bob@example.com", "bob@gmail.com" ] } |
$not |
$not是元条件,即可以在任何其他条件之上
|
> db.users.find({username:{$not:{$in:["joe", "bob"]}}}) { "_id" : ObjectId("551907fc1d802f6252d90e86"), "username" : "jim", "emails" : [ "jim@gmail.com", "jim@hotmail.com" ] } |
数组查询
|
|
|
$all
| 通过多个元素来匹配数组:找出包含"apple", "banana"的文档 |
> db.food.find()
{ "_id" : ObjectId("551913ce1d802f6252d90e87"), "fruit" : [ "banana", "apple", "orange" ] }
{ "_id" : ObjectId("551914241d802f6252d90e88"), "fruit" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5519142f1d802f6252d90e89"), "fruit" : [ "peach", "apple", "cherry" ] }
> db.food.find({fruit:{$all:["apple", "banana"]}})
{ "_id" : ObjectId("551913ce1d802f6252d90e87"), "fruit" : [ "banana", "apple", "orange" ] }
{ "_id" : ObjectId("551914241d802f6252d90e88"), "fruit" : [ "banana", "apple", "cherry" ] }
|
查询数组下标指定的元素
|
数组下标是以0开始的
|
> db.food.find()
{ "_id" : ObjectId("551913ce1d802f6252d90e87"), "fruit" : [ "banana", "apple", "orange" ] }
{ "_id" : ObjectId("551914241d802f6252d90e88"), "fruit" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5519142f1d802f6252d90e89"), "fruit" : [ "peach", "apple", "cherry" ] }
> db.food.find({"fruit.2":"cherry"})
{ "_id" : ObjectId("551914241d802f6252d90e88"), "fruit" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5519142f1d802f6252d90e89"), "fruit" : [ "peach", "apple", "cherry" ] }
|
$size | 查询指定长度的数组,$size不能和其他查询子句组合,比如$gt;解决的方法是增加一个字段count,每次增加($puah)一个元素时,增一($inc),但这种方法不能和$addToSet组合 |
> db.food.find()
{ "_id" : ObjectId("551913ce1d802f6252d90e87"), "fruit" : [ "banana", "apple", "orange" ] }
{ "_id" : ObjectId("551914241d802f6252d90e88"), "fruit" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5519142f1d802f6252d90e89"), "fruit" : [ "peach", "apple", "cherry" ] }
{ "_id" : ObjectId("55192c3a1d802f6252d90e8a"), "fruit" : [ "peach", "cherry" ] }
> db.food.find({fruit:{$size:2}})
{ "_id" : ObjectId("55192c3a1d802f6252d90e8a"), "fruit" : [ "peach", "cherry" ] }
|
$slice
| $slice返回数组的一个子集; 返回前面3行comments db.blog.posts.findOne({}, {comments:{$slice:3}}); 返回后面3行comments db.blog.posts.findOne({}, {comments:{$slice:-3}}) 返回后面1~3行comments db.blog.posts.findOne({}, {comments:{$slice:[1, 3]}}) |
> db.blog.posts.findOne({}, {comments:{$slice:3}})
{
"_id" : ObjectId("5518b70d1d802f6252d90e7f"),
"comments" : [
{
"comment" : "good post",
"votes" : 1
},
{
"comment" : "i thought it was too short",
"author" : "Claire",
"votes" : 3
},
{
"author" : "Jim",
"comment" : "free watches",
"votes" : -1
}
],
"content" : "...",
"title" : "A blog"
}
|
内嵌文档的查询
|
采用
"name.first"
这种方式可以指定一个字段查询
|
> db.people.find()
{ "_id" : ObjectId("55193a202dda99b3237676bf"), "name" : { "first" : "Joe", "last" : "Schmoe" }, "age" : 45 }
> db.people.find({"name.first": "Joe"})
{ "_id" : ObjectId("55193a202dda99b3237676bf"), "name" : { "first" : "Joe", "last" : "Schmoe" }, "age" : 45 }
|
|
采用
{name:{first:"Joe", last:"Schmoe"}}
这种方式要指定内嵌文档中所有的字段
|
> db.people.find({name:{first:"Joe", last:"Schmoe"}})
{ "_id" : ObjectId("55193a202dda99b3237676bf"), "name" : { "first" : "Joe", "last" : "Schmoe" }, "age" : 45 }
|
参考资料: 《MongoDB权威指南》