Mongodb使用$<identifier>過濾更新數(shù)組元素的示例代碼
定義
帶有過濾器的位置操作符$<identifier>定義數(shù)組中數(shù)據(jù)更新時,只符合identifier定義條件的元素才可以更新。其中<identifier>作為查詢條件,需要在arrayFilters中定義。語法如下
{<update operator>: { "<array>.$<identifier>": value}}, { arrayFilters: [{<identifier>: <condition>}]}
搭配使用arrayFiters,更新數(shù)組元素中符合過濾條件的數(shù)組。
db.collection.updateMany( { <query conditions>}, { <update operator>: { "<array>.$[<identifier>]": value}}, { arrayFilters: [{<identifier>: <condition>}]} )
其中identifier,是一小寫字母開頭的字符串,只能包含小寫字母和數(shù)字
行為
- 自mongodb5.0開始,UPDATE操作按照字段名稱的字典順序更新字段。當(dāng)字段中包含數(shù)字時,按照數(shù)字順序依次更新字段。當(dāng)然,對一個文檔的多個字段操作,是原子性的。
- 在arrayFilters中,不能包含文本索引操作符$expr, $text, $where
- 當(dāng)使用{upsert: true}并產(chǎn)生insert行為時,數(shù)據(jù)查詢過濾條件必須包含一個針對操作數(shù)組字段的精確查詢。這樣才可以在update語句中,使用$[identifier]操作符 。按照下面的更新語句,來定義查詢和使用$[identifier]操作符。
db.collection.update( {myArray: [5,8]},//查詢過濾條件中必須包含myArray數(shù)組 {$set: {"myArray.$[element]": 10}},//更新字段名稱為myArray的數(shù)組中所有元素 {upsert: true, arrayFilters: [{element: 0}]} )
當(dāng)沒有文檔符合查詢條件時,該操作向數(shù)據(jù)庫中插入一份新的文檔。
{ "_id": ObjectId(...), "myArray": [10, 10]}
當(dāng)數(shù)據(jù)更新操作中沒有包含該數(shù)組的精確查詢時,若數(shù)據(jù)集中不包含符合查詢條件的數(shù)據(jù)記錄,更新插入操作會報錯。下面的兩個數(shù)據(jù)更新操作會報錯。
db.emptyCollection.updateOne( {}, { $set: {"myArray.$[element]": 10}}, { upsert: true, arrayFilters: [{element: 9}]} ) WriteError({ "index" : 0, "code" : 2, "errmsg" : "The path 'myArray' must exist in the document in order to apply array updates.", "op" : { "q" : { }, "u" : { "$set" : { "myArray.$[]" : 10 } }, "multi" : false, "upsert" : true } })
- $[]操作符可以用在多層嵌套數(shù)組當(dāng)中。
應(yīng)用
更新符合arrayFilters條件的數(shù)組元素
向students表插入數(shù)據(jù)。其中g(shù)rades字段是數(shù)字類型的數(shù)組。
db.students.insertMany([ { "_id" : 1, "grades" : [ 95, 92, 90 ] }, { "_id" : 2, "grades" : [ 98, 100, 102 ] }, { "_id" : 3, "grades" : [ 95, 110, 100 ] } ])
構(gòu)建數(shù)據(jù)更新語句,將大于100分的數(shù)據(jù)更新為100.
db.students.updateMany( {}, { $set: {"grades.$[element]": 100}}, { arrayFilters: [{"element": {$gte: 100}}]} )
更新數(shù)組中符合arrayFilter條件的文檔
使用$[identifier]操作符,能夠更新數(shù)組內(nèi)包含的文檔字段值。$[identifier]操作符與點操作符一起,指定數(shù)組內(nèi)文檔元素字段名稱,實現(xiàn)字段值的更改。
db.collection.updateOne( {<query selector>}, { <update operator>: {"array.$[identifier].field":value}} { arrayFilters: [{<identifier>: <condition>}]} )
構(gòu)建students2 集合,其中g(shù)rades是文檔型數(shù)組。
db.students2.insertMany([ { "_id" : 1, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 6 }, { "grade" : 85, "mean" : 90, "std" : 4 }, { "grade" : 85, "mean" : 85, "std" : 6 } ] }, { "_id" : 2, "grades" : [ { "grade" : 90, "mean" : 75, "std" : 6 }, { "grade" : 87, "mean" : 90, "std" : 3 }, { "grade" : 85, "mean" : 85, "std" : 4 } ] }])
更新符合多個查詢條件的數(shù)組元素
向students3集合插入數(shù)據(jù)
db.students3.insertMany([ { "_id" : 1, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 6 }, { "grade" : 85, "mean" : 100, "std" : 4 }, { "grade" : 85, "mean" : 100, "std" : 6 } ] }, { "_id" : 2, "grades" : [ { "grade" : 90, "mean" : 100, "std" : 6 }, { "grade" : 87, "mean" : 100, "std" : 3 }, { "grade" : 85, "mean" : 100, "std" : 4 } ] } ])
構(gòu)建數(shù)據(jù)更新語句,要求將grades數(shù)組中,grade大于等于30同時std大于等于5的元素std值減少1
db.students3.updateMany( {}, {$inc: {"grades.$[elem].std": -1}}, {arrayFilters: [{"elem.grade": {$gte: 80}, "elem.std": {$gte: 5}}]} )
使用反向操作符查詢更新數(shù)組中的文檔元素
向集合alumni插入數(shù)據(jù)
db.alumni.insertMany([{ "_id": 1, "name": "Christine Franklin", "degrees": [ { "level": "Master" }, { "level": "Bachelor" } ], }, { "_id": 2, "name": "Reyansh Sengupta", "degrees": [ { "level": "Bachelor" } ], } ])
構(gòu)建數(shù)據(jù)更新語句,要求將degrees數(shù)組中l(wèi)evel=Bachelor以外的元素,添加字段gradcampaign,并將字段值設(shè)置為1.
db.alumni.updateMany( {}, {$set: {"degrees.$[degree].gradcampaign": 1}}, { arrayFilters: [ {"degree.level": {$ne: "Bachelor"}}]} )
與$[]集合使用更新數(shù)組內(nèi)嵌套數(shù)組元素值
向集合students4插入數(shù)據(jù)。其中g(shù)rades是文檔數(shù)組。文檔的questions字段是數(shù)字數(shù)組類型。
db.students4.insertOne( { "_id" : 1, "grades" : [ { type: "quiz", questions: [ 10, 8, 5 ] }, { type: "quiz", questions: [ 8, 9, 6 ] }, { type: "hw", questions: [ 5, 4, 3 ] }, { type: "exam", questions: [ 25, 10, 23, 0 ] }, ] } )
構(gòu)建數(shù)據(jù)更新語句,要求更新grades數(shù)組中,type字段值為quiz的文檔,將questions數(shù)組中大于等于8的元素值增加2.
db.students4.updateMany( {}, { $inc: {"grades.$[t].questions.$[score]": 2}}, {arrayFilters: [{"t.type": "quiz"}, {"score": {$gte: 8}}]} )
其中,$[t]和$[score]中括號當(dāng)中,不需要添加空格,否則mongodb會報錯 。
WriteError({ "index" : 0, "code" : 2, "errmsg" : "No array filter found for identifier ' t ' in path 'grades.$[ t ].questions.$[score]'", "op" : { "q" : { }, "u" : { "$inc" : { "grades.$[ t ].questions.$[score]" : 2 } }, "multi" : true, "upsert" : false, "arrayFilters" : [ { "t.type" : "quiz" }, { "score" : { "$gte" : 8 } } ] } })
構(gòu)建數(shù)據(jù)更新語句,要求更新grades數(shù)組中的所有文檔,將questions數(shù)組中大于等于8的元素值增加2.
{ "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }
以上就是Mongodb使用$<identifier>過濾更新數(shù)組元素的示例代碼的詳細內(nèi)容,更多關(guān)于Mongodb $<identifier>過濾元素的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法
副本集是MongoDB的主從復(fù)制中的重要功能,經(jīng)常被用來作額外的備份,這里我們就來詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法,首先還是來回顧一下MongoDB中副本集的基本知識:2016-07-07MongoDB中實現(xiàn)多表聯(lián)查的實例教程
數(shù)據(jù)庫應(yīng)用在我們的生活中是很常見的,在編輯一些應(yīng)用以及軟件的時候都需要用到數(shù)據(jù)庫來存儲數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MongoDB中實現(xiàn)多表聯(lián)查的相關(guān)資料,需要的朋友可以參考下2022-07-07