Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a field to every document in a collection in MongoDB Compass?

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?

like image 949
redwytnblak Avatar asked Nov 25 '19 18:11

redwytnblak


1 Answers

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.

like image 143
prasad_ Avatar answered Nov 15 '22 10:11

prasad_