Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update field in exact element array in MongoDB

I have a document structured like this:

{     _id:"43434",     heroes : [         { nickname : "test",  items : ["", "", ""] },         { nickname : "test2", items : ["", "", ""] },     ] } 

Can I $set the second element of the items array of the embedded object in array heros with nickname "test" ?

Result:

{     _id:"43434",     heroes : [         { nickname : "test",  items : ["", "new_value", ""] }, // modified here         { nickname : "test2", items : ["", "", ""] },     ] } 
like image 915
Denis Ermolin Avatar asked May 03 '12 13:05

Denis Ermolin


People also ask

How do you update a field in an array in MongoDB?

You can use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.

Is it possible to update MongoDB field using value of another field?

Starting from MongoDB 4.2 you can perform Updates with an Aggregation Pipeline. An aggregation pipeline enables more expressive updates including calculated fields and references to other field values in the same document.


2 Answers

You need to make use of 2 concepts: mongodb's positional operator and simply using the numeric index for the entry you want to update.

The positional operator allows you to use a condition like this:

{"heroes.nickname": "test"} 

and then reference the found array entry like so:

{"heroes.$  // <- the dollar represents the first matching array key index 

As you want to update the 2nd array entry in "items", and array keys are 0 indexed - that's the key 1.

So:

> db.denis.insert({_id:"43434", heroes : [{ nickname : "test",  items : ["", "", ""] }, { nickname : "test2", items : ["", "", ""] }]}); > db.denis.update(     {"heroes.nickname": "test"},      {$set: {         "heroes.$.items.1": "new_value"     }} ) > db.denis.find() {     "_id" : "43434",      "heroes" : [         {"nickname" : "test", "items" : ["", "new_value", "" ]},         {"nickname" : "test2", "items" : ["", "", "" ]}     ] } 
like image 137
AD7six Avatar answered Oct 10 '22 10:10

AD7six


Try update document in array using positional $,

The positional $ operator facilitates updates to arrays that contain embedded documents. Use the positional $ operator to access the fields in the embedded documents with the dot notation on the $ operator.

db.collection.update(   { "heroes.nickname": "test" },   { $set: { "heroes.$.items.1": "new_value" } },   { multi: true } ); 

Playground

like image 41
turivishal Avatar answered Oct 10 '22 09:10

turivishal