MongoDB查询集合中的文档

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB查询集合中的文档相关的知识,希望对你有一定的参考价值。

MongoDB查询集合中的文档

参考资料:http://blog.csdn.net/qq_36040184/article/details/54355085

测试:

集合中插入数据

> 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表示假,指不存在


本文出自 “10931853” 博客,请务必保留此出处http://wujianwei.blog.51cto.com/10931853/1957835

以上是关于MongoDB查询集合中的文档的主要内容,如果未能解决你的问题,请参考以下文章

需要 mongodb 查询来检查日期是不是在存储在集合文档中的两个日期之间[重复]

MongoDB常用操作--集合2

Nosql中MongoDB简单的查询练习

MongoDB 集合间关联查询后通过$filter进行筛选

MongoDB - 加载文档子集以进行查询

mongodb高级查询