Mongodb $push in nested array Mongodb $push in nested array mongodb mongodb

Mongodb $push in nested array


Probably something like this where ID is your ObjectId. The first {} are necessary to identify your document. It is not required to use an ObjectId as long as you have another unique identifier in your collection.

db.collection.update(    { "_id": ID, "playlists._id": "58"},    { "$push":         {"playlists.$.musics":             {                "name": "test name",                "duration": "4.00"            }        }    })


This way it worked for me!

"playlists.$[].musics":

db.collection.update({ "_id": ID, "playlists._id": "58"},{ "$push":     {"playlists.$[].musics":         {            "name": "test name",            "duration": "4.00"        }    } })

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#position-nested-arrays-filtered