MongoDB数据查询方法干货篇
本文主要给大家介绍了MongoDB数据查询的相关内容,对大家具有一定的参考价值,需要的朋友们一起来学习学习吧。
导入测试数据
在开始之前我们应该先准备数据方便演示,这里我插入的了几条数据,数据如下:
db.user.insertMany( [{ name:'jack', age:22, sex:'Man', tags:['python','c++','c'], grades:[22,33,44,55], school:{ name:'shida', city:'xuzhou' } },{ name:'jhon', age:33, sex:null, tags:['python','java'], grades:[66,22,44,88], school:{ name:'kuangda', city:'xuzhou' } }, { name:'xiaoming', age:33, tags:['python','java'], grades:[66,22,44,88], school:{ name:'kuangda', city:'xuzhou' } } ] )
find()
其中query表示查找的条件,相当于mysql中where子句,projection列出你想要查找的数据,格式为db.collection.find(find(
实例:
下面不带参数的查找,将会查找出所有的结果
db.find().pretty(); //输出结果 { "_id":ObjectId("59056f81299fe049404b2899"), "name":"jack", "age":22, "tags":[ "python", "c++", "c" ], "grades":[ 22, 33, 44, 55 ], "school":{ "name":"shida", "city":"xuzhou" } }
下面找出满足name为jack的数据,并且只输出name,age,这里的_id是默认输出的,如果不想输出将将它设置为0,想要输出那个字段将它设置为1
db.user.find({name:'jack'},{name:1,age:1}) //输出结果 {"_id":ObjectId("59056f81299fe049404b2899"),"name":"jack","age":22} db.user.find({name:'jack'},{name:1,age:1,_id:0}) //输出结果 {"name":"jack","age":22}
**注意:这里的一个projection不能同时指定包括和排除字段,除了排除_id字段。在显式包括字段的映射中,_id字段是唯一一个您可以显式排除的。
查询内嵌文档
上述例子中插入的school数据就表示内嵌文档
完全匹配查询
完全匹配查询表示school中的查询数组必须和插入的数组完全一样,顺序都必须一样才能查找出来
db.user.find({name:'jack',school:{name:'shida',city:'xuzhou'}}); //输出结果 {"_id":ObjectId("59056f81299fe049404b2899"),"name":"jack","age":22,"tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}} //下面是指定输出的字段,这里的school.name表示只输出school文档中name字段,必须加引号 db.user.find({name:'jack',school:{name:'shida',city:'xuzhou'}},{name:1,age:1,'school.name':1}); //输出结果 {"_id":ObjectId("59056f81299fe049404b2899"),"name":"jack","age":22,"school":{"name":"shida"}}
键值对查询
可以通过键值对查询,不用考虑顺序,比如'school.name':'shida',表示查询学校名字为shida的数据,这里的引号是必须要的
db.user.find({'school.name':'shida'},{name:1,school:1}); //输出结果 {"_id":ObjectId("59056f81299fe049404b2899"),"name":"jack","school":{"name":"shida","city":"xuzhou"}}
查询操作符
下面我们将配合查询操作符来执行复杂的查询操作,比如元素查询、逻辑查询、比较查询操作。我们使用下面的比较操作符"$gt"、"$gte"、"$lt"、"$lte"(分别对应">"、">="、"<"、"<=")
实例
下面查询年龄在20-30之间的信息
db.user.find({ age:{$gt:20,$lt:30} }) //输出 {"_id":ObjectId("59056f81299fe049404b2899"),"name":"jack","age":22,"tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}}
$ne
$ne表示不相等,例如查询年龄不等于22岁的信息
db.user.find({age:{$ne:22}}) //输出 {"_id":ObjectId("59057c16f551d8c9003d31e0"),"name":"jhon","age":33,"tags":["python","java"],"grades":[66,22,44,88],"school":{"name":"kuangda","city":"xuzhou"}}
slice
$slice操作符控制查询返回的数组中元素的个数。此操作符根据参数{field:value}指定键名和键值选择出文档集合,并且该文档集合中指定array键将返回从指定数量的元素。如果count的值大于数组中元素的数量,该查询返回数组中的所有元素的。
语法:db.collection.find({field:value},{array:{$slice:count}});
下面将查询grades中的前两个数
db.user.find({name:'jack'},{grades:{$slice:2},name:1,age:1,'school.name':1}); //输出,可以看出这里的grades只输出了前面两个 {"_id":ObjectId("59057c16f551d8c9003d31df"),"name":"jack","age":22,"grades":[22,33],"school":{"name":"shida"}}
下面将输出后3个数据
db.user.find({name:'jhon'},{grades:{$slice:-3},name:1}); //输出 {"_id":ObjectId("59057c16f551d8c9003d31e0"),"name":"jhon","grades":[22,44,88]}
下面介绍指定一个数组作为参数。数组参数使用[skip,limit]格式,其中第一个值表示在数组中跳过的项目数,第二个值表示返回的项目数。
db.user.find({name:'jack'},{grades:{$slice:[2,2]},name:1});//这里将会跳过前面的两个,直接得到后面的两个数据 //输出 {"_id":ObjectId("59057c16f551d8c9003d31df"),"name":"jack","grades":[44,55]}
$exists
如果$exists的值为true,选择存在该字段的文档,若值为false则选择不包含该字段的文档
下面将会查询不存在sex这一项的信息
db.user.find({sex:{$exists:false}}) //结果 {"_id":ObjectId("59058460fe58ed1089f2a5cd"),"name":"xiaoming","age":33,"tags":["python","java"],"grades":[66,22,44,88],"school":{"name":"kuangda","city":"xuzhou"}} db.user.find({sex:{$exists:true}}); //结果 {"_id":ObjectId("59058460fe58ed1089f2a5cb"),"name":"jack","age":22,"sex":"Man","tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}} {"_id":ObjectId("59058460fe58ed1089f2a5cc"),"name":"jhon","age":33,"sex":null,"tags":["python","java"],"grades":[66,22,44,88],"school":{"name":"kuangda","city":"xuzhou"}}
$or
执行逻辑OR运算,指定一个至少包含两个表达式的数组,选择出至少满足数组中一条表达式的文档。
语法:{$or:[{
下面将要查找age等于22或者age等于33的值
db.user.find({$or:[{age:22},{age:33}]}) //结果 {"_id":ObjectId("59058460fe58ed1089f2a5cb"),"name":"jack","age":22,"sex":"Man","tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}} {"_id":ObjectId("59058460fe58ed1089f2a5cc"),"name":"jhon","age":33,"sex":null,"tags":["python","java"],"grades":[66,22,44,88],"school":{"name":"kuangda","city":"xuzhou"}} {"_id":ObjectId("59058460fe58ed1089f2a5cd"),"name":"xiaoming","age":33,"tags":["python","java"],"grades":[66,22,44,88],"school":{"name":"kuangda","city":"xuzhou"}}
下面将会查找出年龄为22或者33并且姓名为jack的人的信息
db.user.find({name:'jack',$or:[{age:33},{age:22}]}) //结果 {"_id":ObjectId("59058460fe58ed1089f2a5cb"),"name":"jack","age":22,"sex":"Man","tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}}
$and
指定一个至少包含两个表达式的数组,选择出满足该数组中所有表达式的文档。$and操作符使用短路操作,若第一个表达式的值为“false”,余下的表达式将不会执行。
语法:{$and:[{
下面将会查找年龄在20-30之间的信息,对于下面使用逗号分隔符的表达式列表,MongoDB会提供一个隐式的$and操作:
db.user.find({$and:[{age:{$gt:20}},{age:{$lt:30}}]}) //上述语句相当于db.user.find({age:{$gt:20},age:{$lt:30}}) //结果 {"_id":ObjectId("59058460fe58ed1089f2a5cb"),"name":"jack","age":22,"sex":"Man","tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}}
$in
匹配键值等于指定数组中任意值的文档。类似sql中in,只要匹配一个value就会输出
语法:{field:{$in:[
下面将会查找grades中存在22,33之间的任意一个数的信息
db.user.find({grades:{$in:[22,33]}}) //输出 {"_id":ObjectId("59058460fe58ed1089f2a5cb"),"name":"jack","age":22,"sex":"Man","tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}} {"_id":ObjectId("59058460fe58ed1089f2a5cc"),"name":"jhon","age":33,"sex":null,"tags":["python","java"],"grades":[66,22,44,88],"school":{"name":"kuangda","city":"xuzhou"}} {"_id":ObjectId("59058460fe58ed1089f2a5cd"),"name":"xiaoming","age":33,"tags":["python","java"],"grades":[66,22,44,88],"school":{"name":"kuangda","city":"xuzhou"}}
$nin
匹配键不存在或者键值不等于指定数组的任意值的文档。类似sql中notin(SQL中字段不存在使用会有语法错误).
查询出grades中不存在100或者44的文档
db.user.find({grades:{$nin:[100,44]}})
$not
执行逻辑NOT运算,选择出不能匹配表达式的文档,包括没有指定键的文档。$not操作符不能独立使用,必须跟其他操作一起使用
语法:{field:{$not:{}}}
查询年龄不大于30的信息
db.user.find({age:{$not:{$gt:30}}}) //输出 {"_id":ObjectId("59058460fe58ed1089f2a5cb"),"name":"jack","age":22,"sex":"Man","tags":["python","c++","c"],"grades":[22,33,44,55],"school":{"name":"shida","city":"xuzhou"}}
迭代游标的查询
学过高级语言的朋友都知道迭代的问题,像java,下面使用迭代的方法查询
varcursor=db.usr.find(); //这里使用迭代输出所有的数据 while(cursor.hasNext())//这里的hasNext()是判断是否下一个中还有可迭代的值,如果没有返回false { printjson(cursor.next());//这里的cursor.next是迭代的输出,printjson是代替print(tojson()) } printcursor.count()//输出其中有多少个数据 cursor.forEach(printjson);//forEach输出 vardocument=cursor.toArray();//将迭代对象转换成数组 printdocument[0];//以数组的形式输出
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。