Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an object inside an array with PyMongo

I am wondering how do you update a nested array with PyMongo/MongoDB by selecting a document(row) and then going into the nested array and selecting a specific object.

{
    "_id"    : "12345",
    "name"   : "John Doe,
    "mylist" : [
         {
            "nested_id" : "1",
            "data1"     : "lorem ipsum",
            "data2"     : "stackoverflow",
            "data3"     : "james bond"
         },
         {
            "nested_id" : "2",
            "data1"     : "lorem ipsum",
            "data2"     : "stackoverflow",
            "data3"     : "james bond"
         },
         {
            ....
         }     
      ]
}

and then lets say you pass a discretionary with the elements you want to update. In this example only update data1 and data3

data = {
   "data1" : "new lorem",
   "data3" : "goldeneye"       
} 

I have tried with the following syntax, but with no success.

db.testing.find_and_modify(
            query={"_id": "12345", 'mylist.nested_id' : "1"},
            update={"$set": {'mylist' : data}})

what it should look like after the update

{
        "_id"    : "12345",
        "name"   : "John Doe,
        "mylist" : [
             {
                "nested_id" : "1",
                "data1"     : "new lorem",
                "data2"     : "stackoverflow",
                "data3"     : "goldeneye"
             },
             {
                "nested_id" : "2",
                "data1"     : "lorem ipsum",
                "data2"     : "stackoverflow",
                "data3"     : "james bond"
             },
             {
                ....
             }     
          ]
    }
like image 422
Sigils Avatar asked Mar 03 '15 09:03

Sigils


People also ask

How do you update an object in an array?

To update an object's property in an array of objects, use the map() method to iterate over the array. On each iteration, check if the current object is the one to be updated. If it is, modify the object and return the result, otherwise return the object as is. Copied!

How do you update an object inside an object in MongoDB?

For this, use findOneAndUpdate() in MongoDB. The findOneAndUpdate() method updates a single document based on the filter and sort criteria.

How do you update an element 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.


Video Answer


1 Answers

Use "dot notation" and the positional operator in the update portion. Also transform your input to match the "dot notation" form for the key representation:

# Transform to "dot notation" on explicit field
for key in data:
    data["mylist.$." + key] = data[key]
    del data[key]

# Basically makes
# { 
#      "mylist.$.data1": "new lorem",
#      "mylist.$.data3": "goldeneye"
# }

db.testing.find_and_modify(
    query = {"_id": "12345", 'mylist.nested_id' : "1"},
    update = { "$set": data }
)

So that will transpose $ to the actual matched element position from the query portion of the update. The matched array element will be updated and using "dot notation" only the mentioned fields will be affected.

Have no idea what "service" is supposed to mean in this context and I am just treating it as a "transcribing error" since you are clearly trying to match an array element in position.

That could be cleaner, but this should give you the general idea.

like image 89
Neil Lunn Avatar answered Oct 22 '22 16:10

Neil Lunn