首页 > 嗟来之食 > MongoDb进阶实践之六 MongoDB查询命令详述(补充)
2019
06-20

MongoDb进阶实践之六 MongoDB查询命令详述(补充)

一、引言         上一篇文章我们已经介绍了MongoDB数据库的查询操作,但是并没有介绍全,随着自己的学习的深入,对查询又有了新的东西,决定补充进来。如果大家想看上一篇有关MongoDB查询的文章,可以点击这里《MongoDb进阶实践之四 MongoDB查询命令详述》,第一篇文章详细介绍了有关MongoDB查询的有关命令和操作符。废话不多说,今天主要的任务就是把有关MongoDB查询命令的新的东西补充进来,文章的序号,我是接着上一篇文章来的,没有重头来说,这样大家也就知道今天是有关MongoDB查询之四的补充了。好了,马上开始我们今天的写作吧。二、MongDB查询的详解(补充)          我比较直接,直接上内容,有关查询的每个方法都有示例代码,应该不是很难。大家注意,序号不是从1开始的,因为是补充的,大家可以结合上一篇文章一起来看。             12、$in 操作符:在一个数组中查询匹配的文档,查询条件的值可以有多个,条件可以满足一个或者多个                    语法:db.collectionName.find({<key>:{$in:[value1,value2…valuen]}})                    参数说明:                       key:要操作的字段名称                       $in:操作符,在一个数组中选择符合条件的文档                       value1-n:查询条件要满足的值,可以有多个值,可以满足一个,两个,或者多个                    $in操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "userName" : "lilei", "sex" : "1", "age" : "21", "address" : "guangxi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "userName" : "HanMeiMei", "sex" : "0", "age" : "18", "address" : "JiangXi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }

//查询结果
> db.students.find({"age":{"$in":["21","18"]}})
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "userName" : "lilei", "sex" : "1", "age" : "21", "address" : "guangxi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "userName" : "HanMeiMei", "sex" : "0", "age" : "18", "address" : "JiangXi" }
>

             13、$nin 操作符:和$in操作符相反,选择不符合数组条件的所有文档                    语法:db.collectionName.find({<key>:{$nin:[value1,value2…valuen]}})                    参数说明:                       key:要操作的字段名称                       $nin:操作符,在一个数组中选择不符合条件的文档                       value1-n:查询条件要满足的值,可以有多个值,可以满足一个,两个,或者多个                    $nin操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "userName" : "lilei", "sex" : "1", "age" : "21", "address" : "guangxi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "userName" : "HanMeiMei", "sex" : "0", "age" : "18", "address" : "JiangXi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }

//查询结果
> db.students.find({"age":{"$nin":["26"]}})
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "userName" : "lilei", "sex" : "1", "age" : "21", "address" : "guangxi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "userName" : "HanMeiMei", "sex" : "0", "age" : "18", "address" : "JiangXi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }

             14、$and 操作符:该操作符用于连接多个条件,只有当每个条件必须都是为true,整个条件的集合才会返回true,表示条件成立,然后就会返回符合条件的文档                    语法:db.collectionName.find({$and:[{query1},{query2}…{queryn}]})                    参数说明:                       $and:操作符,每个条件都必须返回true,整个条件集合才回返回true;如果有一个条件返回false,整个条件结果就是false。                       query1-n:具体的查询条件,可以有多个查询条件,只有所有条件返回true,整个条件集合才回返回ture,表示成功。                    $and操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "userName" : "lilei", "sex" : "1", "age" : "21", "address" : "guangxi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "userName" : "HanMeiMei", "sex" : "0", "age" : "18", "address" : "JiangXi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }

//查询结果
db.students.find({"$and":[{"age":"21"},{"sex":"1"}]})
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "userName" : "lilei", "sex" : "1", "age" : "21", "address" : "guangxi" }
>

             15、$not 操作符:该操作符用于对当前的条件取反,要和其他操作符配合使用。比如,我可以取性别不是男的所有的文档,也可以获取年龄不包含44和55岁的人的文档                   语法:db.collectionName.find({<key>:{"$not":{"$in":[value1,value2…valuen]}}})                    参数说明:                       key:在字段是要进行条件筛选的字段名。                       $not:操作符,对条件进行取反,通常会和其他操作符配合使用。                       value1-n:具体需要过滤的值,可以是多个值,$in操作符表示符合这些条件,$not操作符就是对$in操作符的条件取反                   $not操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "userName" : "lilei", "sex" : "1", "age" : "21", "address" : "guangxi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "userName" : "HanMeiMei", "sex" : "0", "age" : "18", "address" : "JiangXi" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }

//查询结果,查询年龄不是21和18岁的其他人
> db.students.find({"age":{"$not":{"$in":["21","18"]}}})
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }
>

             16、$all 操作符:是针对数组字段操作的,可以完成类似模糊查询的效果,并且和字段值的顺序无关。一般Mongodb数组字段的查询,都是精确查询,并且和字段的顺序有关。                  语法:db.collectionName.find({<key>:{$all:[value1,value2…valuen]}})                    参数说明:                       key:要操作的字段名称,并且该字段是数组字段                       $all:操作符,在一个数组中选择符合条件的文档                       value1-n:查询条件要满足的值,可以有多个值,可以满足一个,两个,或者多个                    $all操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }
{ "_id" : ObjectId("5adb4b5df82775fbd989b852"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb4b83f82775fbd989b859"), "frut" : [ "apple", "banana", "orange" ] }
>

//查询结果 无值
> db.students.find({"frut":["apple","orange"]})
>

//查询结果 增加了$all操作符的,orange是第三个元素
> db.students.find({"frut":{"$all":["apple","orange"]}})
{ "_id" : ObjectId("5adb4b83f82775fbd989b859"), "frut" : [ "apple", "banana", "orange" ] }

 
             17、$size 操作符:该操作符是根据数组字段元素的个数作为查询条件的,可以理解为查询数组字段中具有number个元素的文档                   语法:db.collectionName.find({<key>:{$size:number}})                    参数说明:                       key:要操作的字段名称,该字段必须是数组字段                       $size:操作符,根据数组元素个数来作为查询条件                       number:表示几个元素,不是第几个,可以理解为我们查询数组字段中有number个值的文档                    $size操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }
{ "_id" : ObjectId("5adb4b5df82775fbd989b852"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb4b83f82775fbd989b859"), "frut" : [ "apple", "banana", "orange" ] }
>

//查询结果,查询只有一个元素的文档,没有
> db.students.find({"frut":{"$size":1}})

//查询结果,查询只有2个元素的文档
> db.students.find({"frut":{"$size":2}})
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb4b5df82775fbd989b852"), "frut" : [ "apple", "banana" ] }

//查询结果,查询只有3个元素的文档
> db.students.find({"frut":{"$size":3}})
{ "_id" : ObjectId("5adb4b83f82775fbd989b859"), "frut" : [ "apple", "banana", "orange" ] }
>

             18、$slice 操作符:该操作符可以从数组字段的头部或者尾部获取指定个数的元素的文档                   语法:db.collectionName.find({query},{<key>:{$slice:number}})                    参数说明:                       key:要操作的字段名称,该字段必须是数组字段                       $slice:操作符,可以从数组的头部或者尾部取指定数量的元素                       number:这个数字表示要获取的数组字段元素的个数,正负表示从头部还是从尾部来获取                    $slice操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }
{ "_id" : ObjectId("5adb4b5df82775fbd989b852"), "frut" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5adb4b83f82775fbd989b859"), "frut" : [ "apple", "banana", "orange" ] }

//查询结果,从头部获取一个元素的文档
> db.students.find({},{"frut":{"$slice":1}})
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "frut" : [ "apple" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "frut" : [ "apple" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }
{ "_id" : ObjectId("5adb4b5df82775fbd989b852"), "frut" : [ "apple" ] }
{ "_id" : ObjectId("5adb4b83f82775fbd989b859"), "frut" : [ "apple" ] }

//查询结果,从尾部获取一个元素的文档
> db.students.find({},{"frut":{"$slice":-1}})
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb40"), "frut" : [ "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb41"), "frut" : [ "banana" ] }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb42"), "userName" : "ZhangFei", "sex" : "1", "age" : "33", "address" : "HeBei" }
{ "_id" : ObjectId("5adb3dbda62a80dd1239bb43"), "userName" : "GuanYu", "sex" : "1", "age" : "26", "address" : "ShanXi" }
{ "_id" : ObjectId("5adb4b5df82775fbd989b852"), "frut" : [ "banana" ] }
{ "_id" : ObjectId("5adb4b83f82775fbd989b859"), "frut" : [ "orange" ] }

             19、$elemMatch 操作符:该操作符也是针对数组字段来进行的,我们可以针对数组中的元素做判断,是否满足某一个条件,满足就显示记录,不满足就不做操作。                   语法:db.collectionName.find({<key>:{$elemMatch:{query}}})                    参数说明:                       key:要操作的字段名称,该字段必须是数组字段。                       $elemMatch:操作符,对数组字段中的元素做判别,看看是否满足特定的条件                       query:该查询条件是针对数组字段中元素进行操作的。                    $elemMatch操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。                    示例代码:

//原始数据
> db.students.find()
{ "_id" : ObjectId("5adb555ca62a80dd1239bb44"), "userName" : "Bob", "values" : [ 1, 2, 3, 4, 5, 6 ] }
{ "_id" : ObjectId("5adb5568a62a80dd1239bb45"), "userName" : "Bob", "values" : 1 }
{ "_id" : ObjectId("5adb556ea62a80dd1239bb46"), "userName" : "Bob", "values" : 2 }
{ "_id" : ObjectId("5adb5571a62a80dd1239bb47"), "userName" : "Bob", "values" : 3 }
{ "_id" : ObjectId("5adb5575a62a80dd1239bb48"), "userName" : "Bob", "values" : 4 }
{ "_id" : ObjectId("5adb5578a62a80dd1239bb49"), "userName" : "Bob", "values" : 5 }
{ "_id" : ObjectId("5adb557ca62a80dd1239bb4a"), "userName" : "Bob", "values" : 6 }

//如果不做限制,数组字段和数字字段都查出来
> db.students.find({"values":{"$gt":1,"$lt":4}})
{ "_id" : ObjectId("5adb555ca62a80dd1239bb44"), "userName" : "Bob", "values" : [ 1, 2, 3, 4, 5, 6 ] }
{ "_id" : ObjectId("5adb556ea62a80dd1239bb46"), "userName" : "Bob", "values" : 2 }
{ "_id" : ObjectId("5adb5571a62a80dd1239bb47"), "userName" : "Bob", "values" : 3 }

//如果我只想针对数组字段操作,可以这样
> db.students.find({"values":{"$elemMatch":{"$gt":1,"$lt":4}}})
{ "_id" : ObjectId("5adb555ca62a80dd1239bb44"), "userName" : "Bob", "values" : [ 1, 2, 3, 4, 5, 6 ] }

 
             20、内嵌文档的查询:如果按一般方式,由于字段结构的不同,查询结果也会有异,如果结构不同,但是内容相同的情况,我们就要使用文档对象的方式来执行查询,摆脱了文档结构的束缚。                   语法:<outKey>.<innerKey>                    参数说明:                       outKey:外部文档(父文档)要操作的字段名称                       innerKey:内部文档(子文档)要操作的字段名称                    示例代码:

//原始数据
> db.names.find()
{ "_id" : ObjectId("5adb5915a62a80dd1239bb4b"), "name" : { "firstName" : "liu", "lastName" : "lei" } }
{ "_id" : ObjectId("5adb5937a62a80dd1239bb4c"), "name" : { "lastName" : "lei", "firstName" : "liu" } }
{ "_id" : ObjectId("5adb5944a62a80dd1239bb4d"), "name" : { "firstName" : "zhang", "lastName" : "san" } }
{ "_id" : ObjectId("5adb5951a62a80dd1239bb4e"), "name" : { "firstName" : "li", "lastName" : "si" } }
{ "_id" : ObjectId("5adb595ba62a80dd1239bb4f"), "name" : { "firstName" : "wang", "lastName" : "wu" } }

//一般查询:查询结果,大家看好了,其实第一条和第二条两个内容是完全一样的,只是顺序不一样,如果按一般方式查询,只能获取一条记录
> db.names.find({"name":{"firstName":"liu","lastName":"lei"}})
{ "_id" : ObjectId("5adb5915a62a80dd1239bb4b"), "name" : { "firstName" : "liu", "lastName" : "lei" } }

//文档对象查询:查询结果,大家看好了,其实第一条和第二条两个内容是完全一样的,只是顺序不一样,如果按一般方式查询,只能获取一条记录
> db.names.find({"name.firstName":"liu","name.lastName":"lei"})
{ "_id" : ObjectId("5adb5915a62a80dd1239bb4b"), "name" : { "firstName" : "liu", "lastName" : "lei" } }
{ "_id" : ObjectId("5adb5937a62a80dd1239bb4c"), "name" : { "lastName" : "lei", "firstName" : "liu" } }

             好了,以上就是新增加进来的,以后学了新的东西在更新吧。主要是上班时间也不是很多,主要是靠周末或者晚上,一些东西很容易时间就飞了,时间控制不好,第二天就没精神了。            三、总结           今天就写到这里了,有关查询和更新的内容还很多,今天只是针对MongoDB的查询新增了一些操作符和相关语法。由于时间不是很充裕,所以这个进度,有可能就慢了,慢工出细活,也希望自己能更深入肌理,明白一二,如果有了心得我还会继续更新的。不忘初衷,继续努力吧。                                                                                     

最后编辑:
作者:
这个作者貌似有点懒,什么都没有留下。

留下一个回复