MongoDB 数组查询

MongoDB在文档上支持数组,其次数组上能够实现嵌套,以及数组元素也能够文档。所以,对于文档上数组的操做,MongoDB提供不少种不一样的方式,包括数组的查询,数组元素的添加删除等等。本文主要描述数组查询,供你们参考。html

1、演示环境及数据

> db.version()
		3.2.11
		
		> db.users.insertMany(
				  [
				     {
				       _id: 1,
				       name: "sue",
				       age: 19,
				       type: 1,
				       status: "P",
				       favorites: { artist: "Picasso", food: "pizza" },
				       finished: [ 17, 3 ],
				       badges: [ "blue", "black" ],
				       points: [
				          { points: 85, bonus: 20 },
				          { points: 85, bonus: 10 }
				       ]
				     },
				     {
				       _id: 2,
				       name: "bob",
				       age: 42,
				       type: 1,
				       status: "A",
				       favorites: { artist: "Miro", food: "meringue" },
				       finished: [ 11, 25 ],
				       badges: [ "green" ],
				       points: [
				          { points: 85, bonus: 20 },
				          { points: 64, bonus: 12 }
				       ]
				     },
				     {
				       _id: 3,
				       name: "ahn",
				       age: 22,
				       type: 2,
				       status: "A",
				       favorites: { artist: "Cassatt", food: "cake" },
				       finished: [ 6 ],
				       badges: [ "blue", "red" ],
				       points: [
				          { points: 81, bonus: 8 },
				          { points: 55, bonus: 20 }
				       ]
				     },
				     {
				       _id: 4,
				       name: "xi",
				       age: 34,         
				       type: 2,         
				       status: "D",
				       favorites: { artist: "Chagall", food: "chocolate" },
				       finished: [ 5, 11 ],
				       badges: [ "red", "black" ],
				       points: [
				          { points: 53, bonus: 15 },
				          { points: 51, bonus: 15 }
				       ]
				     },
				     {
				       _id: 5,
				       name: "xyz",
				       age: 23,
				       type: 2,
				       status: "D",
				       favorites: { artist: "Noguchi", food: "nougat" },
				       finished: [ 14, 6 ],
				       badges: [ "orange" ],
				       points: [
				          { points: 71, bonus: 20 }
				       ]
				     },
				     {
				       _id: 6,
				       name: "abc",
				       age: 43,
				       type: 1,
				       status: "A",
				       favorites: { food: "pizza", artist: "Picasso" },
				       finished: [ 18, 12 ],
				       badges: [ "black", "blue" ],
				       points: [
				          { points: 78, bonus: 8 },
				          { points: 57, bonus: 7 }
				       ]
				     }
				  ]
				)

2、演示数组查询

###一、数组元素模糊匹配web

//以下示例,数组字段badges每一个包含该元素black的文档都将被返回
		> db.users.find({badges:"black"},{"_id":1,badges:1})
		{ "_id" : 1, "badges" : [ "blue", "black" ] }
		{ "_id" : 4, "badges" : [ "red", "black" ] }
		{ "_id" : 6, "badges" : [ "black", "blue" ] }

###二、数组元素精确(全)匹配mongodb

//以下示例,数组字段badges的值为["black","blue"]的文档才能被返回(数组元素值和元素顺序全匹配)
		> db.users.find({badges:["black","blue"]},{"_id":1,badges:1})
		{ "_id" : 6, "badges" : [ "black", "blue" ] }

###三、经过数组下标返回指定的文档数组

数组的下标从0开始,指定下标值则返回对应的文档
		//以下示例,返回数组badges中第一个元素值为black的文档
		> db.users.find({"badges.1":"black"},{"_id":1,badges:1})
		{ "_id" : 1, "badges" : [ "blue", "black" ] }
		{ "_id" : 4, "badges" : [ "red", "black" ] }

###四、范围条件任意元素匹配查询app

//查询数组finished的元素值既大于15,又小于20的文档
		> db.users.find( { finished: { $gt: 15, $lt: 20}},{"_id":1,finished:1})
		{ "_id" : 1, "finished" : [ 17, 3 ] }
		{ "_id" : 2, "finished" : [ 11, 25 ] }
		{ "_id" : 6, "finished" : [ 18, 12 ] }
		
		//下面插入一个新的文档,仅包含单个数组元素
		> db.users.insert({"_id":7,finished:[19]})
		WriteResult({ "nInserted" : 1 })
		
		//再次查询,新增的文档也被返回,补充:仅一个元素知足了这两个条件也被返回@20181010
		//感谢网友Land提出。
		> db.users.find( { finished: { $gt: 15, $lt: 20}},{"_id":1,finished:1})
		{ "_id" : 1, "finished" : [ 17, 3 ] }
		{ "_id" : 2, "finished" : [ 11, 25 ] }
		{ "_id" : 6, "finished" : [ 18, 12 ] }
		{ "_id" : 7, "finished" : [ 19 ] }

###五、数组内嵌文档查询svg

//查询数组points元素1内嵌文档键points的值小于等于55的文档(精确匹配)
		> db.users.find( { 'points.0.points': { $lte: 55}},{"_id":1,points:1})
		{ "_id" : 4, "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }

    //查询数组points内嵌文档键points的值小于等于55的文档,此处经过.成员的方式实现
		> db.users.find( { 'points.points': { $lte: 55}},{"_id":1,points:1})
		{ "_id" : 3, "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }
		{ "_id" : 4, "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }

###六、数组元素操做符$elemMatchpost

做用:数组值中至少一个元素知足全部指定的匹配条件
		语法:  { <field>: { $elemMatch: { <query1>, <query2>, ... } } }
		说明:  若是查询为单值查询条件,即只有<query1>,则无需指定$elemMatch

		//以下示例,为无需指定$elemMatch情形
		//查询数组内嵌文档字段points.points的值为85的文档
		> db.users.find( { "points.points": 85},{"_id":1,points:1})
		{ "_id" : 1, "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }
		{ "_id" : 2, "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }
		
		> db.users.find( { points:{ $elemMatch:{points:85}}},{"_id":1,points:1})
		{ "_id" : 1, "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }
		{ "_id" : 2, "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }
		
		//单数组查询($elemMatch示例)
		> db.scores.insertMany(
		... [{ _id: 1, results: [ 82, 85, 88 ] }, //Author : Leshami
		... { _id: 2, results: [ 75, 88, 89 ] }]) //Blog   : http://blog.csdn.net/leshami
		{ "acknowledged" : true, "insertedIds" : [ 1, 2 ] }
		> db.scores.find({ results: { $elemMatch: { $gte: 80, $lt: 85 } } })
		{ "_id" : 1, "results" : [ 82, 85, 88 ] }
		
		//数组内嵌文档查询示例($elemMatch示例)
		//查询数组内嵌文档字段points.points的值大于等于70,而且bonus的值20的文档(要求2个条件都必须知足)
		//也就是说数组points的至少须要一个元素同时知足以上2个条件,这样的结果文档才会返回
		//下面的查询数组值{ "points" : 55, "bonus" : 20 }知足条件
		> db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20}}},{"_id":1,points:1})
		{ "_id" : 3, "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }

###七、数组元素操做符$all性能

做用:数组值中知足全部指定的匹配条件,不考虑多出的元素以及元素顺序问题
		语法:{ <field>: { $all: [ <value1> , <value2> ... ] } }

		> db.users.find({badges:{$all:["black","blue"]}},{"_id":1,badges:1})
		{ "_id" : 1, "badges" : [ "blue", "black" ] }  //此处查询的结果不考虑元素的顺序
		{ "_id" : 6, "badges" : [ "black", "blue" ] }  //只要包含这2个元素的集合都被返回

		等价的操做方式
		> db.users.find({$and:[{badges:"blue"},{badges:"black"}]},{"_id":1,badges:1})
		{ "_id" : 1, "badges" : [ "blue", "black" ] }
		{ "_id" : 6, "badges" : [ "black", "blue" ] }

###八、数组元素操做符$sizespa

做用:返回元素个数总值等于指定值的文档
		语法:db.collection.find( { field: { $size: 2 } } );
		说明:$size不支持指定范围,而是一个具体的值。此外针对$size,没有相关可用的索引来提升性能

		//查询数组badges包含1个元素的文档		
		> db.users.find({badges:{$size:1}},{"_id":1,badges:1})
		{ "_id" : 2, "badges" : [ "green" ] }
		{ "_id" : 5, "badges" : [ "orange" ] }
		
		//查询数组badges包含2个元素的文档
		> db.users.find({badges:{$size:2}},{"_id":1,badges:1})
		{ "_id" : 1, "badges" : [ "blue", "black" ] }
		{ "_id" : 3, "badges" : [ "blue", "red" ] }
		{ "_id" : 4, "badges" : [ "red", "black" ] }
		{ "_id" : 6, "badges" : [ "black", "blue" ] }

###九、数组元素操做符$slice.net

做用:用于返回指定位置的数组元素值的子集(是数值元素值得一部分,不是全部的数组元素值)
		示例:db.collection.find( { field: value }, { array: {$slice: count } } );		

		//建立演示文档
		> db.blog.insert(
		... {_id:1,title:"mongodb unique index",
		... comment: [
		... {"name" : "joe","content" : "nice post."},
		... {"name" : "bob","content" : "good post."},
		... {"name" : "john","content" : "greatly."}]}
		... )
		WriteResult({ "nInserted" : 1 })
		
		//经过$slice返回集合中comment数组第一条评论
		> db.blog.find({},{comment:{$slice:1}}).pretty()
		{
		        "_id" : 1,
		        "title" : "mongodb unique index",
		        "comment" : [
		                {
		                        "name" : "joe",
		                        "content" : "nice post."
		                }
		        ]
		}
		
		//经过$slice返回集合中comment数组最后一条评论
		> db.blog.find({},{comment:{$slice:-1}}).pretty()
		{
		        "_id" : 1,
		        "title" : "mongodb unique index",
		        "comment" : [
		                {
		                        "name" : "john",
		                        "content" : "greatly."
		                }
		        ]
		}
		
		//经过$slice返回集合中comment数组特定的评论(能够理解为分页)
		//以下查询,返回的是第2-3条评论,第一条被跳过
		> db.blog.find({},{comment:{$slice:[1,3]}}).pretty()
		{
		        "_id" : 1,
		        "title" : "mongodb unique index",
		        "comment" : [
		                {
		                        "name" : "bob",
		                        "content" : "good post."
		                },
		                {
		                        "name" : "john",
		                        "content" : "greatly."
		                }
		        ]
		}

###十、$占位符,返回数组中第一个匹配的数组元素值(子集)

使用样式:
				db.collection.find( { <array>: <value> ... },
				                    { "<array>.$": 1 } )
				db.collection.find( { <array.field>: <value> ...},
				                    { "<array>.$": 1 } )
				                    
		使用示例
		> db.students.insertMany([
				{ "_id" : 1, "semester" : 1, "grades" : [ 70, 87, 90 ] },
				{ "_id" : 2, "semester" : 1, "grades" : [ 90, 88, 92 ] },
				{ "_id" : 3, "semester" : 1, "grades" : [ 85, 100, 90 ] },
				{ "_id" : 4, "semester" : 2, "grades" : [ 79, 85, 80 ] },
				{ "_id" : 5, "semester" : 2, "grades" : [ 88, 88, 92 ] },
				{ "_id" : 6, "semester" : 2, "grades" : [ 95, 90, 96 ] }])						                    
		
		//经过下面的查询可知,仅仅只有第一个大于等于85的元素值被返回
		//也就是说$占位符返回的是数组的第一个匹配的值,是数组的子集
		> db.students.find( { semester: 1, grades: { $gte: 85 } },
		... { "grades.$": 1 } )
		{ "_id" : 1, "grades" : [ 87 ] }
		{ "_id" : 2, "grades" : [ 90 ] }
		{ "_id" : 3, "grades" : [ 85 ] }
		
		
		> db.students.drop()
		
		//使用新的示例数据
		> db.students.insertMany([
				{ "_id" : 7, semester: 3, "grades" : [ { grade: 80, mean: 75, std: 8 },
		                                       { grade: 85, mean: 90, std: 5 },
		                                       { grade: 90, mean: 85, std: 3 } ] },
		    { "_id" : 8, semester: 3, "grades" : [ { grade: 92, mean: 88, std: 8 },
		                                       { grade: 78, mean: 90, std: 5 },
		                                       { grade: 88, mean: 85, std: 3 } ] }])
		
		//下面的查询中,数组的元素为内嵌文档,一样如此,数组元素第一个匹配的元素值被返回
		> db.students.find(
		...    { "grades.mean": { $gt: 70 } },
		...    { "grades.$": 1 }
		... )
		{ "_id" : 7, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 } ] }
		{ "_id" : 8, "grades" : [ { "grade" : 92, "mean" : 88, "std" : 8 } ] }

3、小结
a、数组查询有精确和模糊之分,精确匹配须要指定数据元素的所有值
b、数组查询能够经过下标的方式进行查询
c、数组内嵌套文档能够经过.成员的方式进行查询
d、数组至少一个元素知足全部指定的匹配条件能够使用$elemMatch
e、数组查询中返回元素的子集能够经过$slice以及 f 占位符来实现 f、 all知足全部指定的匹配条件,不考虑多出的元素以及元素顺序问题

DBA牛鹏社(SQL/NOSQL/LINUX)