【MongoDB初识】-条件操作符

1、条件>,<,>=,<=在MongoDB中的写法

  >:$gt,<:$lt,>=:$gte,<=:$lte,<>:$ne

  具体使用方法:

db.class.find({"filed":{$gt:value}})  filed>value

db.class.find({"filed":{$lt:value}})   filed<value

db.class.find({"filed":{$gte:value}})   filed>=value

db.class.find({"filed":{$lte:value}})  filed<=value

db.class.find({"filed":{$gt:value,$lt:value1}})  value1>filed>value

db.class.find({"filed":{$ne:5}})    filed<>value

db.class.find({"stuCount":{$gt:10}})
db.class.find({"stuCount":{$gte:10}})
db.class.find({"stuCount":{$lt:10}})
db.class.find({"stuCount":{$lte:10}})
db.class.find({"stuCount":{$lt:10,$gt:5}})
db.class.find({"stuCount":{$ne:5}})

2、$all匹配所有

    这个操作符跟 SQL 语法的 in 类似,但不同的是, in 只需满足( )内的某一个值即可, 而$all 必
  须满足[ ]内的所有值

  db.class.find({stuCount:{$all:[6,8]}}) 查询结果为空

  db.class.find({stuCount:{$all:[6]}}) 可以查出一条,等同于db.class.find({"stuCount":6})

3、$in包含

  与 sql 标准语法的用途是一样的,即要查询的是一系列枚举值的范围内

  db.class.find({stuCount:{$in:[6,8]}})

4、$nin不包含

  db.class.find({stuCount:{$nin:[6,8]}})

5、判断字段是否存在

  db.class.find({stuCount:{$exists:true}})

6、NULL值处理

  db.class.find({name:null}) 会查询出没有name字段的数据

  db.class.find({name:{"$in":[null],"$exists":true}}) 查询结果为有name字段,且name=null

7、$mod取余

  db.class.find({stuCount:{$mod:[7,2]}})  对7取余等于2

 

转载于:https://www.cnblogs.com/lb12081116/p/4843868.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值