Monday, August 13, 2012

Removing stuff from embedded arrays in MongoDB

How to remove items from embedded list "items" by regular expression /item/ ???

db.example.update(
  { _id: ObjectId("5024d352ef2ff4c4a103db5f") }, 
  { $pull: { items: { $regex: /item/ } } }
)

Do the same but on all documents in collection:

db.example.update({}, { $pull: { items: { $regex: /item/ } } }, false, true)

Oh I so wish it'd be in the official docs - I spent 2 days trying to figure it out!

No comments: