khairul.ikhwan
khairul.ikhwan

Reputation: 527

Mongo push to array inside array

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?

Upvotes: 8

Views: 12117

Answers (2)

Rafiq
Rafiq

Reputation: 11485

if you got into error mongodb The positional operator did not find the match needed from the query. then you need to use $elemMatch to select the parent array first.

await Booking.findOneAndUpdate(
              {
                _id: data.id,
                products: {
                  $elemMatch: {
                    id: product.id,
                  },
                },
              },
              {
                $push: {
                  'products.$.slots': {
                    $each: product.slots.map((slot) => {
                      return { id: slot.id, discount: slot.discount };
                    }),
                  },
                },
              },
              { new: true }
            );
          

Upvotes: 0

Remon van Vliet
Remon van Vliet

Reputation: 18625

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]"}}}
)

Upvotes: 9

Related Questions