Rupesh Yadav Rupesh Yadav - 3 years ago 114
Node.js Question

How to insert in nested Array in mongodb

I see this in Mongodb $push in nested array

What playlist.$.music represent.??

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

Answer Source

Since the $ operator is used for updating, quoting from the MongoDB documentation on $ for update,

The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.

So in the code above, playlists.$.musics part only states to update the property music of first document that matches the criteria on the first pipeline.

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download