push multiple items to array mongodb code example
Example 1: mongodb add multiple element to array
db.students.update(
{ name: "joe" },
{ $push: { scores: { $each: [ 90, 92, 85 ] } } }
)
Example 2: mongodb push to arry in update
db.collection.update({_id:xx}, {$push:{letters : {$each:['first one'], $position:0}}})
Example 3: mongodb push element in nested array
db.collection.update(
{ "_id": ID, "playlists._id": "58"},
{ "$push":
{"playlists.$.musics":
{
"name": "test name",
"duration": "4.00"
}
}
}
)
Example 4: mongodb push to arry in update
db.collection.update({_id:xx}, {$pop:{letters : -1}})
Example 5: insert item into list mongodb
db.col.update(
{ name: 'doc', 'list.id': 2 },
{$push: {'list.$.items': {id: 5, name: 'item5'}}}
)