这篇文章主要介绍MongoDB增删改查之查询怎么实现,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

详述1.简单查询:

>db.t1.find(){"_id":ObjectId("585ce007d993c80e8713c7bd"),"x":1,"j":1}{"_id":ObjectId("585ce007d993c80e8713c7be"),"x":4,"j":2}{"_id":ObjectId("585ce007d993c80e8713c7bf"),"x":2,"j":3}{"_id":ObjectId("585ce008d993c80e8713c7c0"),"x":4,"j":4}{"_id":ObjectId("585ce023d993c80e8713c7c2"),"x":3}2.排序查询:

>db.t1.find().sort({j:-1})---"1"表示升序,“-1”表示降序{"_id":ObjectId("585ce008d993c80e8713c7c0"),"x":4,"j":4}{"_id":ObjectId("585ce007d993c80e8713c7bf"),"x":2,"j":3}{"_id":ObjectId("585ce007d993c80e8713c7be"),"x":4,"j":2}{"_id":ObjectId("585ce007d993c80e8713c7bd"),"x":1,"j":1}{"_id":ObjectId("585ce023d993c80e8713c7c2"),"x":3}3. 条件查询:

>db.t1.find({x:2}){"_id":ObjectId("585ce007d993c80e8713c7bf"),"x":2,"j":3}4.多条件查询

>db.t1.find({x:2,j:3}){"_id":ObjectId("585ce007d993c80e8713c7bf"),"x":2,"j":3}5.比较条件查询

>db.t1.find({j:{$gt:2,$lte:4}})---查询j字段大于2,小于等于4的记录{"_id":ObjectId("585ce007d993c80e8713c7bf"),"x":2,"j":3}{"_id":ObjectId("585ce008d993c80e8713c7c0"),"x":4,"j":4}

比较运算符:

$gt:大于

$lt:小于

$gte:大于等于

$lte:小于等于

$ne:不等于

6.多个值in查询(类似IN)

>db.t1.find({j:{$in:[1,4]}})---j=1或j=4的记录,不包括可以用$nin{"_id":ObjectId("585ce007d993c80e8713c7bd"),"x":1,"j":1}{"_id":ObjectId("585ce008d993c80e8713c7c0"),"x":4,"j":4}7.多个值and查询(类似AND)

>db.t1.find({x:1,j:{$gt:1}})---x=1andj>1条件记录{"_id":ObjectId("585ce007d993c80e8713c7be"),"x":1,"j":2}{"_id":ObjectId("585ce007d993c80e8713c7bf"),"x":1,"j":3}8.多个值or查询(类似OR)

>db.t1.find({$or:[{x:4},{j:{$lte:2}}]})---x=4orj<=2的条件记录{"_id":ObjectId("585ce007d993c80e8713c7bd"),"x":1,"j":1}{"_id":ObjectId("585ce007d993c80e8713c7be"),"x":1,"j":2}{"_id":ObjectId("585ce008d993c80e8713c7c0"),"x":4,"j":4}9.多个值or and 查询

>db.t1.find({x:1,$or:[{j:{$lt:2}},{j:3}]})---x=1and(j<2orj=3){"_id":ObjectId("585ce007d993c80e8713c7bd"),"x":1,"j":1}{"_id":ObjectId("585ce007d993c80e8713c7bf"),"x":1,"j":3}10.查询表中不包括 j 字段的记录

>db.t6.find({j:{$exists:false}})---查询包含j字段的话,就是$exists:true{"_id":ObjectId("585ce023d993c80e8713c7c2"),"x":3}11.只返回某个字段或某些字段

>db.t1.find({x:4},{j:1})---默认显示所有字段值,第二个大括号使用j:1表示只返回j字段,但主键_id默认显示{"_id":ObjectId("585ce008d993c80e8713c7c0"),"j":4}{"_id":ObjectId("585ce007d993c80e8713c7be"),"j":2}12.如何想要消除主键_id的返回

>db.t1.find({x:4},{j:1,_id:0}){"j":4}{"j":2}嵌套子文档查询

原数据表:

>db.t6.find(){"_id":1,"x":2,"kk":{"deviceID":222,"city":"Tianjin"}}{"_id":2,"x":3,"kk":{"deviceID":333,"city":"Beijing"}}{"_id":3,"x":2,"kk":{"deviceID":234}}

查询嵌套子文档中的字段,可以用点来连接外字段与内嵌字段,可能不太明白是什么意思,没关系,直接看如下举例即可明白;

13.根据子文档中某几个字段的值进行匹配查询:

>db.t6.find({kk:{deviceID:222,city:"Tianjin"}})---查询kk子文档中deviceID=222且city:"Tianjin"的记录{"_id":1,"x":2,"kk":{"deviceID":222,"city":"Tianjin"}}

或者使用如下形式

>db.t6.find({"kk.deviceID":222,"kk.city":"Tianjin"})---与上一个方法一样的效果,注意双引号的使用{"_id":1,"x":2,"kk":{"deviceID":222,"city":"Tianjin"}}14.查询嵌套文档中不存在city字段得数据

>db.getCollection('t6').find({"kk.city":{$exists:false}}){"_id":3,"x":2,"kk":{"deviceID":234}}15.查询结果只想返回子文档中的某些字段值

>db.t6.find({},{"kk.deviceID":1,_id:0}){"kk":{"deviceID":222}}{"kk":{"deviceID":333}}{"kk":{"deviceID":234}}

另:当然关于嵌套子文档还有更多更复杂的查询语句,主要还是看你嵌套文档中的数据结构而定,结构越复杂,那么需要写的查询语句就越复杂。

数组查询

MongoDB中数组的查询也是比较繁琐的,因为毕竟其他关系型数据库中不包含此种数据类型。不过没关系,我尽量将我能想到的场景罗列于此,方便你我查找。

原数据表:

>db.t5.find(){"_id":1,"x":2,"Course":["English","Math"],"score":[1,3]}{"_id":2,"x":3,"Course":["Math","English"],"score":[18,12]}{"_id":3,"x":4,"Course":["English"],"score":[98,1]}{"_id":4,"x":5,"Course":["Yuwen","English"],"score":[45,46]}{"_id":5,"x":7,"Course":["Math"],"score":[99,100]}16.精确查询数组中元素

要求:使用{<field>:<value>}形式;

举例1:匹配一个元素

>db.t5.find({Course:"Math"})---查询到的是所有包含“Math”元素的行{"_id":1,"x":2,"Course":["English","Math"],"score":[1,3]}{"_id":2,"x":3,"Course":["Math","English"],"score":[18,12]}{"_id":5,"x":7,"Course":["Math"],"score":[99,100]}

举例2:精确查询多元素

>db.t5.find({Course:["Math","English"]}){"_id":2,"x":3,"Course":["Math","English"],"score":[18,12]}

注意:上述语句只会查询到一行,而另外一笔也包含“Math”和“English”的数据虽然包含的元素一样,但是元素顺序不一致。所以未被列出

举例3: 精确查询一个元素(按元素位置)

>db.t5.find({"Course.1":"English"})---精确查询数组中第2个元素是“English”{"_id":2,"x":3,"Course":["Math","English"],"score":[18,12]}{"_id":4,"x":5,"Course":["Yuwen","English"],"score":[45,46]}17.指定多条件查询数组

要求:使用$elemMatch操作符;

举例1:只要求数组中有一个元素满足所有条件

>db.t5.find({score:{$elemMatch:{$gt:15,$lt:20}}})---也就只有18>15,且18<20{"_id":2,"x":3,"Course":["Math","English"],"score":[18,12]}

举例2: 要求数组中各个元素只满足一部分筛选条件,但是组合起来可以满足所有条件

>db.t5.find({score:{$gt:15,$lt:20}}){"_id":2,"x":3,"Course":["Math","English"],"score":[18,12]}{"_id":3,"x":4,"Course":["English"],"score":[98,1]}

另:上述举例,你可能会迷糊,为什么[98,1]可以入选呢?因为1<20,且98>15,所以满足查询条件,明白了吧?

18.获取数组字段的数组长度

>db.t5.find({_id:5}).forEach(function(x){varscore=x.score;varscoreLenth=score.length;print(scoreLenth);});2---返回结果

查看数组不为空:

>db.t9.find({arrary:{$elemMatch:{$ne:null}})正则表达式

>db.t4.find(){"_id":ObjectId("58aa8e4c151e4fd0af703688"),"name":"Jack","addr":"tianjin"}{"_id":ObjectId("58aa8e5e151e4fd0af703689"),"name":"Tom","addr":"beijing"}{"_id":ObjectId("58aa9c25151e4fd0af70368a"),"name":"NickJ"}19.查询name以J开头的记录

>db.t4.find({name:/^J/})---查询以J开头的记录{"_id":ObjectId("58aa8e4c151e4fd0af703688"),"name":"Jack","addr":"tianjin"}20.查询不以J开头的记录

>db.t4.find({name:{$not:/^J/}})---查询不以J开头的记录{"_id":ObjectId("58aa8e5e151e4fd0af703689"),"name":"Tom","addr":"beijing"}{"_id":ObjectId("58aa9c25151e4fd0af70368a"),"name":"NickJ"}21.查询name重包含J的记录

>db.t4.find({name:/J/})---查询name重包含J的记录{"_id":ObjectId("58aa8e4c151e4fd0af703688"),"name":"Jack","addr":"tianjin"}{"_id":ObjectId("58aa9c25151e4fd0af70368a"),"name":"NickJ"}查询Null或者不存在的字段

原集合数据:

>db.t8.find(){"_id":99,"name":null}{"_id":100}22.查询name:null的记录:

>db.t8.find({name:null})---出现这样的结果,是因为这样的查询方式会返回name=null且不存在name字段的记录{"_id":99,"name":null}{"_id":100}23.只查询name:null的数据,不包含name字段的不返回

>db.t8.find({name:{$type:10}})---可以使用$type,10其实就代表了null这个数据类型{"_id":99,"name":null}24.只返回不包含name字段的数据

>db.t8.find({name:{$exists:false}})---如果只查询存在name字段的记录,则$exists:true{"_id":100}迭代查询

find()查询之后返回的是一个cursor游标,在mongo shell默认情况下迭代20次显示前20个文档记录

如果使用变量迭代结果集的话:

>vara=db.t6.find()---定义变量>while(a.hasNext()){---判断变量游标位置...printjson(a.next());---打印游标内容...}{"_id":1,"x":2,"kk":{"deviceID":222,"city":"Tianjin"}}{"_id":2,"x":2,"kk":{"deviceID":222,"addr":"Heping"}}

或者使用forEach()

>vara=db.t6.find()>a.forEach(printjson){"_id":1,"x":2,"kk":{"deviceID":222,"city":"Tianjin"}}{"_id":2,"x":2,"kk":{"deviceID":222,"addr":"Heping"}}

使用toArry()逐一查询结果集

>vara=db.t6.find()---定义变量>vardoc=a.toArray()---使用toArray()生成数组结果集>doc[0]---查询结果集中第一个文档{"_id":1,"x":2,"kk":{"deviceID":222,"city":"Tianjin"}}

以上是“MongoDB增删改查之查询怎么实现”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!