Mongodb最大的功能之一就是它支持动态查询,就跟传统的关系型数据库查询一样,但是它的查询来的更灵活。
一、 Query Expression Objects:查询表达式对象
查询表达式文档也是一个BSON结构的文档,例如,我们可以用下面的查询语句来查询集合中的所有记录:
db.users.find({})
这里,表达式对象是一个空文档,在查询的时候去去匹配所有的记录。再看:
db.users.find({'last_name': 'Smith'})
这里,我们将会查询出所有“last_name”属性值为“Smith”的文档记录。
二、查询选项
除了查询表达式意外,Mongodb还支持一些额外的参数选项。例如,我们可能仅仅只想返回某些特定的字段值:
//返回除了age字段外的所有字段
> db.user.find({},{age:0});
//返回tags=tennis 除了comments的所有列
db.posts.find( { tags : 'tennis' }, { comments : 0 } );
//返回userid=16的name字段
> db.user.find({userid:16},{name:1});
{ "_id" : 16, "name" : "user16" }
//返回x=john的所有z字段
db.things.find( { x : "john" }, { z : 1 } );
注: _id字段始终都会被返回,哪怕没有明确指定
三、查询条件
1) <, <=, >, >=
1
2
3
4
5
6
7
8
9
10
11
12
|
// 大于: field > value db.collection. find ({ "field" : { $gt: value } } ); // 小于:field < value db.collection. find ({ "field" : { $lt: value } } ); // 大于等于: field >= value db.collection. find ({ "field" : { $gte: value } } ); // 小于等于:field<=value db.collection. find ({ "field" : { $lte: value } } ); |
2) $all
$all操作类似$in操作,但是不同的是,$all操作要求数组里面的值全部被包含在返回的记录里面,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
> use test ; switched to db test > db.things.insert({a:[1,2,3]}); > db.things. find (); { "_id" : ObjectId( "4de73360059e7f4bdf907cfe" ), "a" : [ 1, 2, 3 ] } > db.things. find ({a:{$all:[2,3]}}); { "_id" : ObjectId( "4de73360059e7f4bdf907cfe" ), "a" : [ 1, 2, 3 ] } > db.things. find ({a:{$all:[1,2,3]}}); { "_id" : ObjectId( "4de73360059e7f4bdf907cfe" ), "a" : [ 1, 2, 3 ] } > db.things. find ({a:{$all:[1]}}); { "_id" : ObjectId( "4de73360059e7f4bdf907cfe" ), "a" : [ 1, 2, 3 ] } > db.things. find ({a:{$all:[1,2,3,4]}}); |
3) $exists
$exists操作检查一个字段是否存在,如:
1
2
3
4
5
6
7
8
9
10
11
12
|
> for (var i=0;i<1000;i++) db.user.save({_id:i,name: 'user' +i,userid:i,age:20}); // 包含userid > db.user. find ({userid:{$exists: true }}).limit(5); { "_id" : 0, "name" : "user0" , "userid" : 0, "age" : 20 } { "_id" : 1, "name" : "user1" , "userid" : 1, "age" : 20 } { "_id" : 2, "name" : "user2" , "userid" : 2, "age" : 20 } { "_id" : 3, "name" : "user3" , "userid" : 3, "age" : 20 } { "_id" : 4, "name" : "user4" , "userid" : 4, "age" : 20 } // 不包含sex字段 > db.user. find ({sex:{$exists: false }}).limit(5); |
4) $mod
$mod操作可以让我们简单的进行取模操作,而不需要用到where子句,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//where 子句 > db.user. find ( "this._id%10==1" ).limit(5); { "_id" : 1, "name" : "user1" , "userid" : 1, "age" : 20 } { "_id" : 11, "name" : "user11" , "userid" : 11, "age" : 20 } { "_id" : 21, "name" : "user21" , "userid" : 21, "age" : 20 } { "_id" : 31, "name" : "user31" , "userid" : 31, "age" : 20 } { "_id" : 41, "name" : "user41" , "userid" : 41, "age" : 20 } // $mod操作 > db.user. find ({_id:{$mod:[10,1]}}).limit(5); { "_id" : 1, "name" : "user1" , "userid" : 1, "age" : 20 } { "_id" : 11, "name" : "user11" , "userid" : 11, "age" : 20 } { "_id" : 21, "name" : "user21" , "userid" : 21, "age" : 20 } { "_id" : 31, "name" : "user31" , "userid" : 31, "age" : 20 } { "_id" : 41, "name" : "user41" , "userid" : 41, "age" : 20 } |
5) $ne
$ne意思是not equal,不等于,不用多说,看例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
> db.user. find ().limit(5); { "_id" : 0, "name" : "user0" , "userid" : 0, "age" : 20 } { "_id" : 1, "name" : "user1" , "userid" : 1, "age" : 20 } { "_id" : 2, "name" : "user2" , "userid" : 2, "age" : 20 } { "_id" : 3, "name" : "user3" , "userid" : 3, "age" : 20 } { "_id" : 4, "name" : "user4" , "userid" : 4, "age" : 20 } > db.user. find ({_id:{$ ne :0}}).limit(5); { "_id" : 1, "name" : "user1" , "userid" : 1, "age" : 20 } { "_id" : 2, "name" : "user2" , "userid" : 2, "age" : 20 } { "_id" : 3, "name" : "user3" , "userid" : 3, "age" : 20 } { "_id" : 4, "name" : "user4" , "userid" : 4, "age" : 20 } { "_id" : 5, "name" : "user5" , "userid" : 5, "age" : 20 } |
6) $in
$in操作类似于传统关系数据库中的IN,看例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// 数据库中有所有数组对应的记录 > db.user. find ({_id:{$ in :[2,3,4,5,6]}}).limit(5); { "_id" : 2, "name" : "user2" , "userid" : 2, "age" : 20 } { "_id" : 3, "name" : "user3" , "userid" : 3, "age" : 20 } { "_id" : 4, "name" : "user4" , "userid" : 4, "age" : 20 } { "_id" : 5, "name" : "user5" , "userid" : 5, "age" : 20 } { "_id" : 6, "name" : "user6" , "userid" : 6, "age" : 20 } // 因为数据库中没有_id=1111的记录 > db.user. find ({_id:{$ in :[2,3,4,5,1111]}}).limit(5); { "_id" : 2, "name" : "user2" , "userid" : 2, "age" : 20 } { "_id" : 3, "name" : "user3" , "userid" : 3, "age" : 20 } { "_id" : 4, "name" : "user4" , "userid" : 4, "age" : 20 } { "_id" : 5, "name" : "user5" , "userid" : 5, "age" : 20 } |
7) $nin
$nin跟$in操作相反,看例子:
1
2
3
4
5
6
7
|
// 扣掉_id=1 /2/3/4 的记录 > db.user. find ({_id:{$nin:[1,2,3,4]}}).limit(5); { "_id" : 0, "name" : "user0" , "userid" : 0, "age" : 20 } { "_id" : 5, "name" : "user5" , "userid" : 5, "age" : 20 } { "_id" : 6, "name" : "user6" , "userid" : 6, "age" : 20 } { "_id" : 7, "name" : "user7" , "userid" : 7, "age" : 20 } { "_id" : 8, "name" : "user8" , "userid" : 8, "age" : 20 } |
8) $nor、$or
$nor跟$or相反,不好解释,看例子:
1
2
3
4
5
6
7
8
9
10
11
|
> db.user. find ({$nor:[{_id:2},{name: 'user3' },{userid:4}]}).limit(5); { "_id" : 0, "name" : "user0" , "userid" : 0, "age" : 20 } { "_id" : 1, "name" : "user1" , "userid" : 1, "age" : 20 } { "_id" : 5, "name" : "user5" , "userid" : 5, "age" : 20 } { "_id" : 6, "name" : "user6" , "userid" : 6, "age" : 20 } { "_id" : 7, "name" : "user7" , "userid" : 7, "age" : 20 } > db.user. find ({$or:[{_id:2},{name: 'user3' },{userid:4}]}).limit(5); { "_id" : 2, "name" : "user2" , "userid" : 2, "age" : 20 } { "_id" : 3, "name" : "user3" , "userid" : 3, "age" : 20 } { "_id" : 4, "name" : "user4" , "userid" : 4, "age" : 20 } |
以上所述就是本文的全部内容了,希望大家能够喜欢。