博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB查询集合中的文档
阅读量:6604 次
发布时间:2019-06-24

本文共 7181 字,大约阅读时间需要 23 分钟。

MongoDB查询集合中的文档

参考资料:

测试:

集合中插入数据

> db.chenji.insert({"name":"张三","年纪":"三年级","年龄":"14"})

WriteResult({ "nInserted" : 1 })

> db.chenji.insert({"name":"王三","年纪":"四年级","年龄":"16"})

WriteResult({ "nInserted" : 1 })

> db.chenji.insert({"name":"王七","年纪":"一年级","年龄":"8"})

WriteResult({ "nInserted" : 1 })

> db.chenji.insert({"name":"张巴","年纪":"一年级","年龄":"9"})

WriteResult({ "nInserted" : 1 })

> db.chenji.insert({"name":"谢娜","年纪":"七年级","年龄":"16"})

WriteResult({ "nInserted" : 1 })

> db.chenji.find()

{ "_id" : ObjectId("59990ec402730a37fe1ee79e"),"name" : "建伟", "年纪" : "二年级","年龄" : "12" }

{ "_id" :ObjectId("59987eee7cd6cda607043cd1"), "name" : "李旭","年纪" : "三年级", "年龄" : "13" }

{ "_id" :ObjectId("5998860a7cd6cda607043cd2"), "name" : "张三","年纪" : "三年级", "年龄" : "14" }

{ "_id" : ObjectId("599886227cd6cda607043cd3"),"name" : "王三", "年纪" : "四年级","年龄" : "16" }

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

{ "_id" :ObjectId("5998866e7cd6cda607043cd6"), "name" : "谢娜","年纪" : "七年级", "年龄" : "16" }

 

 

3.1使用条件表达式(<, <=, >, >=,!=)

//大于: field > value

db.collection.find({field:{$gt:value}});

 

db.chenji.find({"年龄":{$gt:"6"}});

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

 

//小于: field < value

db.collection.find({field:{$lt:value}});

> db.chenji.find({"年龄":{$lt:"16"}});

{ "_id" :ObjectId("59990ec402730a37fe1ee79e"), "name" : "建伟","年纪" : "二年级", "年龄" : "12" }

{ "_id" :ObjectId("59987eee7cd6cda607043cd1"), "name" : "李旭","年纪" : "三年级", "年龄" : "13" }

{ "_id" :ObjectId("5998860a7cd6cda607043cd2"), "name" : "张三","年纪" : "三年级", "年龄" : "14" }

 

 

//大于等于: field >=value

db.collection.find({field:{$gte:value}});

> db.chenji.find({"年龄":{$gte:"7"}});

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

> db.chenji.find({"年龄":{$gte:"6"}});

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" : ObjectId("599886537cd6cda607043cd5"),"name" : "张巴", "年纪" : "一年级","年龄" : "9" }

 

//小于等于: field <=value

db.collection.find({field:{$lte:value}});

 

> db.chenji.find({"年龄":{$lte:"13"}});

{ "_id" : ObjectId("59990ec402730a37fe1ee79e"),"name" : "建伟", "年纪" : "二年级","年龄" : "12" }

{ "_id" :ObjectId("59987eee7cd6cda607043cd1"), "name" : "李旭","年纪" : "三年级", "年龄" : "13" }

 

//不等于: field != value

db.collection.find({field:{$ne:value}});

 

db.chenji.find({"name":{$ne:"谢娜"}});

{ "_id" :ObjectId("59990ec402730a37fe1ee79e"), "name" : "建伟","年纪" : "二年级", "年龄" : "12" }

{ "_id" :ObjectId("59987eee7cd6cda607043cd1"), "name" : "李旭","年纪" : "三年级", "年龄" : "13" }

{ "_id" :ObjectId("5998860a7cd6cda607043cd2"), "name" : "张三","年纪" : "三年级", "年龄" : "14" }

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

 

db.chenji.find({"年纪":{$ne:"三年级"}});

{ "_id" :ObjectId("59990ec402730a37fe1ee79e"), "name" : "建伟","年纪" : "二年级", "年龄" : "12" }

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

{ "_id" :ObjectId("5998866e7cd6cda607043cd6"), "name" : "谢娜","年纪" : "七年级", "年龄" : "16" }

 

3.2统计(count)、排序(sort)、分页(skiplimit)

> db.chenji.count();

7

> db.chenji.find().count();

7

 

> db.chenji.find({"年龄":{$lt:"16"}})

{ "_id" :ObjectId("59990ec402730a37fe1ee79e"), "name" : "建伟","年纪" : "二年级", "年龄" : "12" }

{ "_id" :ObjectId("59987eee7cd6cda607043cd1"), "name" : "李旭","年纪" : "三年级", "年龄" : "13" }

{ "_id" :ObjectId("5998860a7cd6cda607043cd2"), "name" : "张三","年纪" : "三年级", "年龄" : "14" }

>

> db.chenji.find({"年龄":{$lt:"16"}}).count();

3

 

> db.chenji.find({"年纪":{$ne:"三年级"}});

{ "_id" :ObjectId("59990ec402730a37fe1ee79e"), "name" : "建伟","年纪" : "二年级", "年龄" : "12" }

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

{ "_id" :ObjectId("5998866e7cd6cda607043cd6"), "name" : "谢娜","年纪" : "七年级", "年龄" : "16" }

> db.chenji.find({"年纪":{$ne:"三年级"}}).count();

5

 

排序:默认是按照自身的id号来排序

> db.chenji.find().sort({"年龄":1});

 

{ "_id" :ObjectId("59990ec402730a37fe1ee79e"), "name" : "建伟","年纪" : "二年级", "年龄" : "12" }

{ "_id" :ObjectId("59987eee7cd6cda607043cd1"), "name" : "李旭","年纪" : "三年级", "年龄" : "13" }

{ "_id" :ObjectId("5998860a7cd6cda607043cd2"), "name" : "张三","年纪" : "三年级", "年龄" : "14" }

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

{ "_id" : ObjectId("5998866e7cd6cda607043cd6"),"name" : "谢娜", "年纪" : "七年级","年龄" : "16" }

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

 

忽略前两个id的记录,查看接着的三条记录

 

> db.chenji.find().skip(2).limit(3);

{ "_id" :ObjectId("5998860a7cd6cda607043cd2"), "name" : "张三","年纪" : "三年级", "年龄" : "14" }

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

 

> db.chenji.find().skip(2).limit(2);

{ "_id" :ObjectId("5998860a7cd6cda607043cd2"), "name" : "张三","年纪" : "三年级", "年龄" : "14" }

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

 

按年龄倒序排序,并且查看第一行记录

 

> db.chenji.find().sort({"年龄":-1}).limit(1)

{ "_id" :ObjectId("599886537cd6cda607043cd5"), "name" : "张巴","年纪" : "一年级", "年龄" : "9" }

按年龄倒序排序,并且查看第二行记录

> db.chenji.find().sort({"年龄":-1}).limit(2)

{ "_id" : ObjectId("599886537cd6cda607043cd5"),"name" : "张巴", "年纪" : "一年级","年龄" : "9" }

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

 

 

> db.chenji.find().sort({"年龄":-1}).skip(2).limit(2).count(1)

2

> db.chenji.find().sort({"年龄":-1}).skip(1).limit(3)

{ "_id" :ObjectId("599886357cd6cda607043cd4"), "name" : "王七","年纪" : "一年级", "年龄" : "8" }

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

{ "_id" :ObjectId("5998866e7cd6cda607043cd6"), "name" : "谢娜","年纪" : "七年级", "年龄" : "16" }

 

3.3$or,相当于关系型数据库中的OR,表示或者的关系

查询name为张三或者年龄为16

> db.chenji.find({$or:[{name:"王三"},{"年龄":16}]}) 

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

> db.chenji.find({$or:[{name:"王三"},{"年龄":12}]})

{ "_id" :ObjectId("599886227cd6cda607043cd3"), "name" : "王三","年纪" : "四年级", "年龄" : "16" }

 

3.4$nor,表示根据条件过滤掉某些数据

 

查询name不是张三或者年龄不是13

>db.chenji.find({$nor:[{name:"李旭"},{"年龄":13}]})

3.5$exists,用于查询集合中存在某个键的文档或不存在某个键的文档

查询customer集合中存在name键的所有文档

$exists:1表示真,指存在

$exists:0表示假,指不存在

 本文转自 wjw555 51CTO博客,原文链接:http://blog.51cto.com/wujianwei/1957835

转载地址:http://alwso.baihongyu.com/

你可能感兴趣的文章
050:navie时间和aware时间详解
查看>>
百度地图车辆运动轨迹
查看>>
使用ReaderWriterLock类实现多用户读/单用户写同步
查看>>
用 Hexo + Next + GitHubPages 搭建漂亮的免费博客
查看>>
Pandas:按条件进行行选择
查看>>
spring boot 自定义规则访问获取内部或者外部静态资源图片
查看>>
springmvc + mybatis + ehcache + redis架构
查看>>
sed指定行范围匹配(转贴!)
查看>>
C#语音朗读文本 — TTS的实现
查看>>
Python正则表达式初识(十)附正则表达式总结
查看>>
APICLOUD 1.1.0 开发环境搭建
查看>>
《Cadence 16.6电路设计与仿真从入门到精通》——导读
查看>>
Confluence 6 如何让我的小组成员知道那些内容是重要的
查看>>
找到一个适合的分布式文件系统之各种分布式文件系统优缺点对比
查看>>
httpd基本配置
查看>>
索引失效的几个原因
查看>>
关于多线程中使用while做循环而不使用if的解释
查看>>
欢迎你,企业基础架构CCIE,RS CCIEv5.0的升级版新时代迎合自动化运维的网工顶级认证...
查看>>
js typoeof用法
查看>>
五险一金,你清楚吗?
查看>>