《MongoDB 高級用法-數組查詢》要點:
本文介紹了MongoDB 高級用法-數組查詢,希望對您有用。如果有疑問,可以聯系我們。
相關主題:非關系型數據庫
《MongoDB 高級用法-數組查詢》是否對您有啟發,歡迎查看更多與《MongoDB 高級用法-數組查詢》相關教程,學精學透。維易PHP學院為您提供精彩教程。
文章出處:http://blog.csdn.net/leshami/article/details/55049891
MongoDB在文檔上支持數組,其次數組上可以實現嵌套,以及數組元素也可以文檔.因此,對于文檔上數組的操作,MongoDB提供很多種不同的方式,包括數組的查詢,數組元素的添加刪除等等.本文主要描述數組查詢,供大家參考.
> 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 } ] } ] )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
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" ] }12345
2、數組元素精確(全)匹配
//如下示例,數組字段badges的值為["black","blue"]的文檔才能被返回(數組元素值和元素順序全匹配) > db.users.find({badges:["black","blue"]},{"_id":1,badges:1}) { "_id" : 6, "badges" : [ "black", "blue" ] }123
3、通過數組下標返回指定的文檔
數組的下標從0開始,指定下標值則返回對應的文檔 //如下示例,返回數組badges中第一個元素值為black的文檔 > db.users.find({"badges.1":"black"},{"_id":1,badges:1}) { "_id" : 1, "badges" : [ "blue", "black" ] } { "_id" : 4, "badges" : [ "red", "black" ] }12345
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 }) //再次查詢,新增的文檔也被返回 > 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 ] }12345678910111213141516
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 } ] }12345678
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 } ] }12345678910111213141516171819202122232425262728
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" ] }1234567891011
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" ] }123456789101112131415
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." } ] }1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
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 } ] }123456789101112131415161718192021222324252627282930313233343536373839404142
a、數組查詢有精確和模糊之分,精確匹配需要指定數據元素的全部值
b、數組查詢可以通過下標的方式進行查詢
c、數組內嵌套文檔可以通過.成員的方式進行查詢
d、數組至少一個元素滿足所有指定的匹配條件可以使用$elemMatch
e、數組查詢中返回元素的子集可以通過$slice以及占位符來實現f、all滿足所有指定的匹配條件,不考慮多出的元素以及元素順序問題
文章出處:http://blog.csdn.net/leshami/article/details/55049891