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 a similar result to what you are looking for. The main difference is that $push will append the new element to the list, whereas $addToSet will add the element if it is not there (a unique set of elements).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)