How to insert an element to MongoDB internal list?

One way of doing it would be with $push:

db.col.update(
    { name: 'doc', 'list.id': 2 }, 
    {$push: {'list.$.items': {id: 5, name: 'item5'}}}
)

http://docs.mongodb.org/manual/reference/operator/push/

You can also replace $push with other operators like (possibly) $addToSet to get the exact results you are looking for.

Leave a Comment