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) <, <=, >, >=
// 大于: 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操作要求数组里面的值全部被包含在返回的记录里面,如:
> 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操作检查一个字段是否存在,如:
> 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子句,如:
//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,不等于,不用多说,看例子:
> 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,看例子:
//数据库中有所有数组对应的记录 > 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操作相反,看例子:
//扣掉_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相反,不好解释,看例子:
> 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 }
以上所述就是本文的全部内容了,希望大家能够喜欢。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- RonaldBrautigam,PeterMasseursandConcertgebouwOrchestraAmsterdam-ShostakovichTheJazz
- 筷子兄弟《老男孩》[WAV+CUE][899M]
- 陈曦《寂寞在唱歌HQCD》2023头版限量编号[WAV+CUE][1G]
- 张惠妹《阿密特》NEW XRCD[WAV+CUE][455M]
- 邓伟标《西游记之悟空》24K金碟限量编号首版[低速原抓WAV+CUE]
- 群星《2024好听新歌40》AI调整音效【WAV分轨】
- 张梦弘《大城小爱HQ》头版限量编号[低速原抓WAV+CUE]
- 张敬轩《MY 1ST COLLECTION》2CD[WAV+CUE][1.7G]
- 张玮伽《夜归人HQⅡ》2024头版限量编号[WAV+CUE][523M]
- 证声音乐图书馆《夏至 爵士境地》[320K/MP3][70.37MB]
- 孙露《同名专辑》限量1:1母盘直刻[低速原抓WAV+CUE]
- 【宝丽金唱片】群星《鼓舞飞扬》WAV+CUE
- 莫扎特弗雷德沃夏克肖斯塔科维奇《钢琴五重奏》(DG24-96)FLAC
- 证声音乐图书馆《夏至 爵士境地》[FLAC/分轨][360.16MB]
- 证声音乐图书馆《日落琴声 x 弦乐》[320K/MP3][71.2MB]