Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo push to array inside array

Tags:

python

mongodb

im new with mongo and so far have no issue using it. Until i stuck at this. I need to push a document to an array inside an array. Can refer to json below.

{
    'user_id':'{1231mjnD-32JIjn-3213}',
    'name':'John',
    'campaigns':
        [
            {
                'campaign_id':3221,
                'start_date':'12-01-2012',
                'messages':
                    [
                        {
                            'message_id':211134,
                            'email':'[email protected]'
                        }
                    ]
            },
            {
                'campaign_id':3222,
                'start_date':'13-01-2012',
                'messages':
                    [
                        {
                            'message_id':315521,
                            'email':'[email protected]'
                        }
                    ]
            }
        ]
}

I want to push one document to an array of messages in a campaigns array(regardless ordering). Meaning that, i need to append new document to an array of messages. And all those messages is from inside another array or campaigns per user. Im using python so my code will be like this.

query = {"user_id" : "{1231mjnD-32JIjn-3213}", "campaigns.campaign_id": 3221}
message = {"message_id":4213122, "email":"[email protected]"}
op = {"$push" : {"campaigns.messages":message}}
mongo.TestDatabase.members.update(query, op)

There is no error upon execution. But the document seems to have no changes(no update made). What am i doing wrong here?

like image 213
khairul.ikhwan Avatar asked Feb 09 '12 11:02

khairul.ikhwan


People also ask

How do I push an array into an array in MongoDB?

In MongoDB, the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element. If the updating field is not an array type field the operation failed.

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.

How do I update 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.


1 Answers

Having arrays inside arrays is almost always a bad idea for a number of reasons. I'd put the campaigns in a dedicated collection so they become top level documents.

That said, you can push to a message array of a specific campaign through :

db.members.update(
    {"user_id" : "{1231mjnD-32JIjn-3213}", "campaigns.campaign_id": 3221},
    {$push:{"campaigns.$.messages":{"message_id":4213122, "email":"[email protected]"}}}
)
like image 164
Remon van Vliet Avatar answered Oct 28 '22 18:10

Remon van Vliet