Mongodb使用$<identifier>過(guò)濾更新數(shù)組元素的示例代碼
定義
帶有過(guò)濾器的位置操作符$<identifier>定義數(shù)組中數(shù)據(jù)更新時(shí),只符合identifier定義條件的元素才可以更新。其中<identifier>作為查詢(xún)條件,需要在arrayFilters中定義。語(yǔ)法如下
{<update operator>: { "<array>.$<identifier>": value}}, { arrayFilters: [{<identifier>: <condition>}]}
搭配使用arrayFiters,更新數(shù)組元素中符合過(guò)濾條件的數(shù)組。
db.collection.updateMany( { <query conditions>}, { <update operator>: { "<array>.$[<identifier>]": value}}, { arrayFilters: [{<identifier>: <condition>}]} )
其中identifier,是一小寫(xiě)字母開(kāi)頭的字符串,只能包含小寫(xiě)字母和數(shù)字
行為
- 自mongodb5.0開(kāi)始,UPDATE操作按照字段名稱(chēng)的字典順序更新字段。當(dāng)字段中包含數(shù)字時(shí),按照數(shù)字順序依次更新字段。當(dāng)然,對(duì)一個(gè)文檔的多個(gè)字段操作,是原子性的。
- 在arrayFilters中,不能包含文本索引操作符$expr, $text, $where
- 當(dāng)使用{upsert: true}并產(chǎn)生insert行為時(shí),數(shù)據(jù)查詢(xún)過(guò)濾條件必須包含一個(gè)針對(duì)操作數(shù)組字段的精確查詢(xún)。這樣才可以在update語(yǔ)句中,使用$[identifier]操作符 。按照下面的更新語(yǔ)句,來(lái)定義查詢(xún)和使用$[identifier]操作符。
db.collection.update( {myArray: [5,8]},//查詢(xún)過(guò)濾條件中必須包含myArray數(shù)組 {$set: {"myArray.$[element]": 10}},//更新字段名稱(chēng)為myArray的數(shù)組中所有元素 {upsert: true, arrayFilters: [{element: 0}]} )
當(dāng)沒(méi)有文檔符合查詢(xún)條件時(shí),該操作向數(shù)據(jù)庫(kù)中插入一份新的文檔。
{ "_id": ObjectId(...), "myArray": [10, 10]}
當(dāng)數(shù)據(jù)更新操作中沒(méi)有包含該數(shù)組的精確查詢(xún)時(shí),若數(shù)據(jù)集中不包含符合查詢(xún)條件的數(shù)據(jù)記錄,更新插入操作會(huì)報(bào)錯(cuò)。下面的兩個(gè)數(shù)據(jù)更新操作會(huì)報(bào)錯(cuò)。
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ù)字類(lèi)型的數(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ù)更新語(yǔ)句,將大于100分的數(shù)據(jù)更新為100.
db.students.updateMany( {}, { $set: {"grades.$[element]": 100}}, { arrayFilters: [{"element": {$gte: 100}}]} )
更新數(shù)組中符合arrayFilter條件的文檔
使用$[identifier]操作符,能夠更新數(shù)組內(nèi)包含的文檔字段值。$[identifier]操作符與點(diǎn)操作符一起,指定數(shù)組內(nèi)文檔元素字段名稱(chēng),實(shí)現(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 } ] }])
更新符合多個(gè)查詢(xún)條件的數(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ù)更新語(yǔ)句,要求將grades數(shù)組中,grade大于等于30同時(shí)std大于等于5的元素std值減少1
db.students3.updateMany( {}, {$inc: {"grades.$[elem].std": -1}}, {arrayFilters: [{"elem.grade": {$gte: 80}, "elem.std": {$gte: 5}}]} )
使用反向操作符查詢(xún)更新數(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ù)更新語(yǔ)句,要求將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ù)字?jǐn)?shù)組類(lèi)型。
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ù)更新語(yǔ)句,要求更新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]中括號(hào)當(dāng)中,不需要添加空格,否則mongodb會(huì)報(bào)錯(cuò) 。
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ù)更新語(yǔ)句,要求更新grades數(shù)組中的所有文檔,將questions數(shù)組中大于等于8的元素值增加2.
{ "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }
以上就是Mongodb使用$<identifier>過(guò)濾更新數(shù)組元素的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Mongodb $<identifier>過(guò)濾元素的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法
副本集是MongoDB的主從復(fù)制中的重要功能,經(jīng)常被用來(lái)作額外的備份,這里我們就來(lái)詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法,首先還是來(lái)回顧一下MongoDB中副本集的基本知識(shí):2016-07-07MongoDB特定類(lèi)型的查詢(xún)語(yǔ)句實(shí)例
在關(guān)系型數(shù)據(jù)庫(kù)中,可以實(shí)現(xiàn)基于表的各種各樣的查詢(xún),下面這篇文章主要給大家介紹了關(guān)于MongoDB特定類(lèi)型查詢(xún)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04MongoDB插入、更新、刪除文檔實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家簡(jiǎn)單介紹了mongodb插入、更新、刪除文檔的方法,需要的的朋友參考下吧2017-04-04mongodb字段值自增長(zhǎng)實(shí)現(xiàn)代碼
這篇文章主要介紹了mongodb字段值自增長(zhǎng)實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01mongodb禁止外網(wǎng)訪問(wèn)及添加賬號(hào)的操作方法
這篇文章主要介紹了mongodb禁止外網(wǎng)訪問(wèn)及添加賬號(hào)的操作方法,需要的朋友可以參考下2017-12-12MongoDB中實(shí)現(xiàn)多表聯(lián)查的實(shí)例教程
數(shù)據(jù)庫(kù)應(yīng)用在我們的生活中是很常見(jiàn)的,在編輯一些應(yīng)用以及軟件的時(shí)候都需要用到數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MongoDB中實(shí)現(xiàn)多表聯(lián)查的相關(guān)資料,需要的朋友可以參考下2022-07-07