MongoDB–数组查询

1、数组元素模糊匹配

//如下示例,数组字段badges每个包含该元素black的文档都将被返回

1
2
3
4
> db.users.find({badges:"black"},{"_id":1,badges:1})
{ "_id" : 1, "badges" : [ "blue", "black" ] }
{ "_id" : 4, "badges" : [ "red", "black" ] }
{ "_id" : 6, "badges" : [ "black", "blue" ] }

2、数组元素精确(全)匹配

//如下示例,数组字段badges的值为[“black”,”blue”]的文档才能被返回(数组元素值和元素顺序全匹配)

1
2
> db.users.find({badges:["black","blue"]},{"_id":1,badges:1})
{ "_id" : 6, "badges" : [ "black", "blue" ] }

3、通过数组下标返回指定的文档

  数组的下标从0开始,指定下标值则返回对应的文档 //如下示例,返回数组badges中第一个元素值为black的文档

1
2
3
> db.users.find({"badges.1":"black"},{"_id":1,badges:1})
{ "_id" : 1, "badges" : [ "blue", "black" ] }
{ "_id" : 4, "badges" : [ "red", "black" ] }

4、范围条件任意元素匹配查询

  //查询数组finished的元素值既大于15,又小于20的文档

1
2
3
4
> 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 ] }

//下面插入一个新的文档,仅包含单个数组元素

1
2
> db.users.insert({"_id":7,finished:[19]})
WriteResult({ "nInserted" : 1 })
1
2
3
4
5
> 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 ] }

5、数组内嵌文档查询

  //查询数组points元素1内嵌文档键points的值小于等于55的文档(精确匹配)

1
2
> 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的文档,此处通过.成员的方式实现

1
2
3
> 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 } ] }

6、数组元素操作符$elemMatch

  作用:数组值中至少一个元素满足所有指定的匹配条件 语法: { : { $elemMatch: { , , … } } } 说明: 如果查询为单值查询条件,即只有,则无需指定$elemMatch //如下示例,为无需指定$elemMatch情形 //查询数组内嵌文档字段points.points的值为85的文档

1
2
3
> 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 } ] }
1
2
3
> 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示例)

1
2
3
4
5
6
> 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 }满足条件

1
2
> 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 } ] }

7、数组元素操作符$all

作用:数组值中满足所有指定的匹配条件,不考虑多出的元素以及元素顺序问题 语法:{ : { $all: [ , … ] } }  

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

等价的操作方式

1
2
3
> db.users.find({$and:[{badges:"blue"},{badges:"black"}]},{"_id":1,badges:1})
{ "_id" : 1, "badges" : [ "blue", "black" ] }
{ "_id" : 6, "badges" : [ "black", "blue" ] }

8、数组元素操作符$size

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

//查询数组badges包含1个元素的文档

1
2
3
> db.users.find({badges:{$size:1}},{"_id":1,badges:1})
{ "_id" : 2, "badges" : [ "green" ] }
{ "_id" : 5, "badges" : [ "orange" ] }

//查询数组badges包含2个元素的文档

1
2
3
4
5
> 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" ] }

9、数组元素操作符$slice

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

//创建演示文档

1
2
3
4
5
6
7
8
> 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数组第一条评论

1
2
3
4
5
6
7
8
9
10
11
> db.blog.find({},{comment:{$slice:1}}).pretty()
{
"_id" : 1,
"title" : "mongodb unique index",
"comment" : [
{
"name" : "joe",
"content" : "nice post."
}
]
}

//通过$slice返回集合中comment数组最后一条评论

1
2
3
4
5
6
7
8
9
10
11
> db.blog.find({},{comment:{$slice:-1}}).pretty()
{
"_id" : 1,
"title" : "mongodb unique index",
"comment" : [
{
"name" : "john",
"content" : "greatly."
}
]
}

//通过$slice返回集合中comment数组特定的评论(可以理解为分页)
//如下查询,返回的是第2-3条评论,第一条被跳过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.blog.find({},{comment:{$slice:[1,3]}}).pretty()
{
"_id" : 1,
"title" : "mongodb unique index",
"comment" : [
{
"name" : "bob",
"content" : "good post."
},
{
"name" : "john",
"content" : "greatly."
}
]
}

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

  使用样式:

1
2
3
4
db.collection.find( { <array>: <value> ... },
{ "<array>.$": 1 } )
db.collection.find( { <array.field>: <value> ...},
{ "<array>.$": 1 } )

使用示例

1
2
3
4
5
6
7
> 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的元素值被返回 //也就是说$占位符返回的是数组的第一个匹配的值,是数组的子集

1
2
3
4
5
> 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()

//使用新的示例数据

1
2
3
4
5
6
7
> 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 } ] }])

//下面的查询中,数组的元素为内嵌文档,同样如此,数组元素第一个匹配的元素值被返回

1
2
3
4
5
6
> 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 } ] }

小结

1、数组查询有精确和模糊之分,精确匹配需要指定数据元素的全部值 2、数组查询可以通过下标的方式进行查询 3、数组内嵌套文档可以通过.成员的方式进行查询 4、数组至少一个元素满足所有指定的匹配条件可以使用$elemMatch 5、数组查询中返回元素的子集可以通过$slice以及占位符来实现f、 占位符来实现f、占位符来实现f、all满足所有指定的匹配条件,不考虑多出的元素以及元素顺序问题

  • 版权声明: 本博客所有文章,未经许可,任何单位及个人不得做营利性使用!转载请标明出处!如有侵权请联系作者。
  • Copyrights © 2015-2024 翟天野

请我喝杯咖啡吧~