Back
How can I add a new field to every document in an existent collection?
I know how to update an existing document's field but not how to add a new field to every document in a collection. How can I do this in the mongo shell?
To add a new field to every document in a MongoDB collection you have to use empty selector, and set the multi flag to true to update all the documents as follows:-
db.your_collection.update( {}, { $set: {"new_field": 1} }, false, true )
db.your_collection.update(
{},
{ $set: {"new_field": 1} },
false,
true
)
31k questions
32.8k answers
501 comments
693 users