mongodb 數組查詢
轉發自:https://blog.csdn.net/leshami/article/details/55049891
一、演示環境及數據
> 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 }
]
}
]
)
二、演示數組查詢
###1、數組元素模糊匹配
//如下示例,數組字段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" ] }
###2、數組元素精確(全)匹配
//如下示例,數組字段badges的值為["black","blue"]的文檔才能被返回(數組元素值和元素順序全匹配)
> db.users.find({badges:["black","blue"]},{"_id":1,badges:1})
{ "_id" : 6, "badges" : [ "black", "blue" ] }
###3、通過數組下標返回指定的文檔
數組的下標從0開始,指定下標值則返回對應的文檔
//如下示例,返回數組badges中第一個元素值為black的文檔
> db.users.find({"badges.1":"black"},{"_id":1,badges:1})
{ "_id" : 1, "badges" : [ "blue", "black" ] }
{ "_id" : 4, "badges" : [ "red", "black" ] }
###4、範圍條件任意元素匹配查詢
//查詢數組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 ] }
###5、數組內嵌文檔查詢
//查詢數組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 } ] }
###6、數組元素操作符$elemMatch
作用:數組值中至少一個元素滿足所有指定的匹配條件
語法: { <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 } ] }
###7、數組元素操作符$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" ] }
###8、數組元素操作符$size
作用:返回元素個數總值等於指定值的文檔
語法: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" ] }
###9、數組元素操作符$slice
作用:用於返回指定位置的數組元素值的子集(是數值元素值得一部分,不是所有的數組元素值)
示例: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."
}
]
}
###10、$占位符,返回數組中第一個匹配的數組元素值(子集)
使用樣式:
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 } ] }
三、小結
a、數組查詢有精確和模糊之分,精確匹配需要指定數據元素的全部值
b、數組查詢可以通過下標的方式進行查詢
c、數組內嵌套文檔可以通過.成員的方式進行查詢
d、數組至少一個元素滿足所有指定的匹配條件可以使用$elemMatch
e、數組查詢中返回元素的子集可以通過$slice以及占位符來實現f、 占位符來實現f、占位符來實現f、all滿足所有指定的匹配條件,不考慮多出的元素以及元素順序問題
---------------------
作者:Leshami
來源:CSDN
原文:https://blog.csdn.net/leshami/article/details/55049891
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
mongodb 數組查詢