evi
evi

Reputation: 874

An efficient way to update dictionary in MongoDB?

I have this MongoDb schema:

tags:{
"image_uid":"",
"faces": [
    {
        "image_uid":"",
        "age_real":""
    }
]}

witch I update with a dictionary

feedbacks = [{
                 'face_uid': '02d42dee-3b66-11e2-b12e-e0cb4e12150c',
                 'age': 23
             },
             {
                 'face_uid': '02d42dee-3b66-11e2-b12e-e0cb4e12150d',
                 'age': 23
             }]

in this way:

 for feedback in feedbacks:
    tags.update(
        {'image_uid': image_uid, 'faces.face_uid': feedback['face_uid']},
        {"$set": {'faces.$.age_real': feedback['age']}}, w=1
    )

There is a more efficient way instead of the for loop?

Upvotes: 2

Views: 1651

Answers (1)

Nicola Iarocci
Nicola Iarocci

Reputation: 6576

Currently MongoDB offers no support for updating multiple array elements at once. However, instead of performing several updates in sequence, you might choose to use the Update if Current pattern, or something similar, to update your document locally and then replace it on the DB.

Also, check the original jira, where you can find a couple work-arounds in the comments.

Upvotes: 1

Related Questions