? MongoDb进阶实践之六 MongoDB查询命令详述(补充)_蜘蛛资讯网 禁肖--禁尾
首页 > 调查 > 正文

玉树步慈呕经贸有限公司_MongoDb进阶实践之六 MongoDB查询命令详述(补充)

一、引言

???????? 上一篇禁肖--禁尾我们已经介绍了MongoDB数据库的查询操作,但是并没有介绍全,随着自己的学习的深入,对查询又有了新的东西,决定补充进来。如果大家想看上一篇有关MongoDB查询的禁肖--禁尾,可以点击这里《MongoDb进阶实践之四 MongoDB查询命令详述》,第一篇禁肖--禁尾详细介绍了有关MongoDB查询的有关命令和操作符。废话不多说,今天主要的任务就是把有关MongoDB查询命令的新的东西补充进来,禁肖--禁尾的序号,我是接着上一篇禁肖--禁尾来的,没有重头来说,这样大家也就知道今天是有关MongoDB查询之四的补充了。好了,马上开始我们今天的写作吧。

二、MongDB查询的详解(补充

????????? 我比较直接,直接上内容,有关查询的每个方法都有示例代码,应该不是很难。大家注意,序号不是从1开始的,因为是补充的,大家可以结合上一篇禁肖--禁尾一起来看。

???????????? 12、$in 操作符:在一个数组中查询匹配的文档,查询条件的值可以有多个,条件可以满足一个或者多个

??????????????????? 语法:db.collectionName.find({:{$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({:{$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({:{"$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({:{$all:[value1,value2...valuen]}})

??????????????????? 参数说明:

?????????????????????? key:要操作的字段名称,并且该字段是数组字段

?????????????????????? $all:操作符,在一个数组中选择符合条件的文档

?????????????????????? value1-n:查询条件要满足的值,可以有多个值,可以满足一个,两个,或者多个

??????????????????? $all操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

??????????????????? 示例代码:

yuan shi shu ju gt 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" cha xun jie guo, cha xun nian ling bu shi 21 he 18 sui de qi ta ren gt 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" gt ???????????? 16 all cao zuo fu: shi zhen dui shu zu zi duan cao zuo de, ke yi wan cheng lei si mo hu cha xun de xiao guo, bing qie he zi duan zhi de shun xu wu guan. yi ban Mongodb shu zu zi duan de cha xun, dou shi jing que cha xun, bing qie he zi duan de shun xu you guan.????????????????? yu fa: db. collectionName. find lt key gt: all: value1, value2... valuen??????????????????? can shu shuo ming:?????????????????????? key: yao cao zuo de zi duan ming cheng, bing qie gai zi duan shi shu zu zi duan?????????????????????? all: cao zuo fu, zai yi ge shu zu zhong xuan ze fu he tiao jian de wen dang?????????????????????? value1n: cha xun tiao jian yao man zu de zhi, ke yi you duo ge zhi, ke yi man zu yi ge, liang ge, huo zhe duo ge??????????????????? all cao zuo fu shi ji yu BSON lei xing lai jian suo ji he zhong pi pei de shu ju lei xing, bing fan hui jie guo.??????????????????? shi li dai ma:

              //原始数据
              > 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({:{$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},{:{$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", " [责任编辑: 杜马]

评论

?
[ 中国首批预订用户参观贾跃亭FF总部:率先试乘FF 91电动车 ]? [ ??? `???? ?` [MK??] ]? [ 小米有上架好产品懒鱼缸:上面的花下的鱼小米鱼缸新浪科技 ]? [ 特斯拉达成Model 3产能目标,做空者亏惨 ]? [ 印度前总理瓦杰帕伊去世 享年93岁 ]? [ 美国迈阿密一过街天桥坍塌:多车被压桥下 已致10人死亡 ]

?
  • 关于我们 | 蜘蛛资讯网 版权所有

    Copyright ? 2019 蜘蛛资讯网 All Rights Reserved