Let’s say that a collection named items:
{ "_id" : "a", "authorization" : { "group1" : "lrw", "group2" : "lr-" } }, { "_id" : "b", "authorization" : { "group1" : "lrw", "group2" : "lr-" } }, { "_id" : "c", "authorization" : { "group1" : "lrw", "group2" : "lr-" } }
Now I would like to add a field to every “authorization”. The field may look like this:
{ "group3" : "lrw" }
Here’s the query that does that:
db.items.find({}).forEach(item => { db.items.update( { _id: item._id }, { $set: { "authorization.group3": "lrw" } } ) });