免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2442 | 回复: 1
打印 上一主题 下一主题

[MongoDB] [原创] MongoDB管理与开发精要《红丸出品》5.1 条件操作符 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-26 21:20 |只看该作者 |倒序浏览
[backcolor=rgb(245, 247, 24]
[backcolor=rgb(245, 247, 24]5.1 条件操作符 5.1 条件操作符
<, <=, >, >= 这个操作符就不用多解释了,最常用也是最简单的
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 } } );  // 小于等于: field <= value
如果要同时满足多个条件,可以这样做
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } );    // value1 < field < value
5.2 $all匹配所有
这个操作符跟SQL语法的in类似,但不同的是, in只需满足( )内的某一个值即可, $all必须满足[ ]内的所有值,例如:
db.users.find({age : {$all : [6, 8]}});
可以查询出  {name: 'David', age: 26, age: [ 6, 8, 9 ] }
但查询不出  {name: 'David', age: 26, age: [ 6, 7, 9 ] }
5.3 $exists判断字段是否存在
查询所有存在age字段的记录
db.users.find({age: {$exists: true}});
查询所有不存在name字段的记录
db.users.find({name: {$exists: false}});
举例如下:
C1表的数据如下:
> db.c1.find();
{ "_id" : ObjectId("4fb4a773afa87dc1bed9432d", "age" : 20, "length" : 30 }
{ "_id" : ObjectId("4fb4a7e1afa87dc1bed9432e", "age_1" : 20, "length_1" : 30 }
查询存在字段age的数据
> db.c1.find({age:{$exists:true}});
{ "_id" : ObjectId("4fb4a773afa87dc1bed9432d", "age" : 20, "length" : 30 }
可以看出只显示出了有age字段的数据,age_1的数据并没有显示出来
5.4 Null值处理
Null值的处理稍微有一点奇怪,具体看下面的样例数据:
> db.c2.find()
{ "_id" : ObjectId("4fc34bb81d8a39f01cc17ef4", "name" : "Lily", "age" : null }
{ "_id" : ObjectId("4fc34be01d8a39f01cc17ef5", "name" : "Jacky", "age" : 23 }
{ "_id" : ObjectId("4fc34c1e1d8a39f01cc17ef6", "name" : "Tom", "addr" : 23 }
其中”Lily”age字段为空,Tom没有age字段,我们想找到age为空的行,具体如下:
> db.c2.find({age:null})              
{ "_id" : ObjectId("4fc34bb81d8a39f01cc17ef4", "name" : "Lily", "age" : null }
{ "_id" : ObjectId("4fc34c1e1d8a39f01cc17ef6", "name" : "Tom", "addr" : 23 }
奇怪的是我们以为只能找到”Lily”,但”Tom”也被找出来了,所以”null”不仅能找到它自身,连不存在age字段的记录也找出来了。那么怎么样才能只找到”Lily”?我们用exists来限制一下即可:
> db.c2.find({age:{"$in":[null], "$exists":true}})
{ "_id" : ObjectId("4fc34bb81d8a39f01cc17ef4", "name" : "Lily", "age" : null }
这样如我们期望一样,只有”Lily”被找出来了。
5.5 $mod取模运算
查询age取模10等于0的数据
db.student.find( { age: { $mod : [ 10 , 1 ] } } )
举例如下:
C1表的数据如下:
> db.c1.find()
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330", "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
查询age取模6等于1的数据
>  db.c1.find({age: {$mod : [ 6 , 1 ] } })
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
可以看出只显示出了age取模6等于1的数据,其它不符合规则的数据并没有显示出来
5.6 $ne不等于
查询x的值不等于3的数据
db.things.find( { x : { $ne : 3 } } );
举例如下:
C1表的数据如下:
> db.c1.find()
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
查询age的值不等于7的数据
> db.c1.find( { age : { $ne : 7 } } );
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
可以看出只显示出了age等于7的数据,其它不符合规则的数据并没有显示出来
5.7 $in包含
sql标准语法的用途是一样的,即要查询的是一系列枚举值的范围内
查询x的值在2,4,6范围内的数据
db.things.find({x:{$in: [2,4,6]}});
举例如下:
C1表的数据如下:
> db.c1.find()
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
查询age的值在7,8范围内的数据
> db.c1.find({age:{$in: [7,8]}});
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
可以看出只显示出了age等于78的数据,其它不符合规则的数据并没有显示出来
5.8 $nin不包含
sql标准语法的用途是一样的,即要查询的数据在一系列枚举值的范围外
查询x的值在2,4,6范围外的数据
db.things.find({x:{$nin: [2,4,6]}});
举例如下:
C1表的数据如下:
> db.c1.find()
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
查询age的值在7,8范围外的数据
> db.c1.find({age:{$nin: [7,8]}});
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
可以看出只显示出了age不等于78的数据,其它不符合规则的数据并没有显示出来
5.9 $size数组元素个数
对于{name: 'David', age: 26, favorite_number: [ 6, 7, 9 ] }记录
匹配db.users.find({favorite_number: {$size: 3}});
不匹配db.users.find({favorite_number: {$size: 2}});
举例如下:
C1表的数据如下:
> db.c1.find()
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
查询age的值在7,8范围外的数据
> db.c1.find({age:{$nin: [7,8]}});
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
可以看出只显示出了age不等于78的数据,其它不符合规则的数据并没有显示出来
5.10 正则表达式匹配
查询不匹配name=B*带头的记录
db.users.find({name: {$not: /^B.*/}});
举例如下:
C1表的数据如下:
> db.c1.find();
{ "_id" : ObjectId("4fb5faaf6d0f9d8ea3fc91a8"), "name" : "Tony", "age" : 20 }
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
查询name不以T开头的数据
> db.c1.find({name: {$not: /^T.*/}});
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
可以看出只显示出了name=Tony的数据,其它不符合规则的数据并没有显示出来

-------------------------------------------------------------------
免费收几个想学数据库IT技术的徒弟(限北京)   
《MongoDB管理与开发精要》、《Redis实战》作者
新浪博客                  http://blog.sina.com.cn/u/2446082491
ChinaUnix.net专家 http://cdhongwan.blog.chinaunix.net
@CD红丸                http://weibo.com/u/2446082491
红丸IT培训群            http://q.weibo.com/1282646



论坛徽章:
0
2 [报告]
发表于 2012-07-26 03:01 |只看该作者
好帖,确实好帖!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP