MongoDB: Adding a new field all documents in a collection

To add a new field to all documents in a MongoDB collection, first connect to the DB using the command line..

mongo myDB

Once connected, run the following command to add a field called isOpen with the value of false.

db.myCollection.update({}, {$set: {"isOpen": false}}, false, true)

Here’s the MongoDB shell syntax for update():

db.collection.update( criteria, objNew, upsert, multi )

criteria – query which selects the record to update;
objNew – updated object or $ operators (e.g., $inc) which manipulate the object
upsert – if this should be an “upsert” operation; that is, if the record(s) do not exist, insert one. Upsert only inserts a single document.
multi – indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.

More info here: