MongoDB $关键字 关系比较符号 $lt $lte $gt $gte $ne
Posted minger_lcm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB $关键字 关系比较符号 $lt $lte $gt $gte $ne相关的知识,希望对你有一定的参考价值。
关系比较符: 小于:$lt 小于或等于:$lte 大于:$gt 大于或等于:$gte 不等于:$ne 属于:$in
查询中常见的 等于 大于 小于 大于等于 小于等于
等于 : 在MongoDB中什么字段等于什么值其实就是 " : " 来搞定 比如 "name" : "jack"
"name" : "jack"
> db.user.find() { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 } { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" } { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 } { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
小于 : 在MongoDB中的 小于 < 号 我们用 : $lt 比如 : "age" : { $lt : 73 } 就是 得到 "age" 小于 73 的数据
> db.user.find({age:{$lt:73}}) { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
大于 : 在MongoDB中的 大于 > 号 我们用 : $gt 比如 : "age" : { $gt : 19 } 就是 得到 "age" 大于 19 的数据
> db.user.find({age:{$gt:19}}) { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 } { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" } { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 } { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
大于等于 : 在MongoDB中的 大于等于 >= 号 我们用 : $gte 比如 : "age" : { $gte : 13 } 就是 得到 "age" 大于等于 13 的数据
> db.user.find({age:{$gte:13}}) { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 } { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" } { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 } { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
小于等于 : 在MongoDB中的 小于等于 <= 号 我们用 : $lte 比如 : "age" : { $lte : 73 } 就是 得到 "age" 小于等于 73 的数据
> db.user.find({age:{$lte:73}}) { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 } { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
不等于:$ne
把age不等于73的所有数据显示
> db.user.find({age:{$ne:73} } ) { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" } { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 } { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
以上是关于MongoDB $关键字 关系比较符号 $lt $lte $gt $gte $ne的主要内容,如果未能解决你的问题,请参考以下文章