7

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?

redwytnblak
  • 143
  • 1
  • 1
  • 10

1 Answers1

8

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.

prasad_
  • 12,755
  • 2
  • 24
  • 36