Is it possible to add a field to every document in a collection in MongoDB Compass? Or is this something that has to be done in the shell?
There is no option in Compass to update all documents with a new field; Compass's "Document tab" has option to modify a document's field or add a new field (modify one document at a time).
This is to be done from the mongo shell or your favorite programming language.
From the shell, to update all documents in a collection with a new field use the db.collection.updateMany() method. For example, db.test.updateMany( { }, { $set: { new_field: "initial value" } } )
.
Once, the documents are updated, these can be viewed from the Compass; just do a refresh in the Documents view / tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With