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?
Is it possible to add a field to every document in a collection in MongoDB Compass?
Asked Answered
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.
this simply works –
Bootlace
There is now an option available in MongoDB compass to update every document inside a collection.
- Select the desired collection and click on update button.
- A dialog box will appear which will show JSON to set the values.
- Add the values in JSON format and update.
{ $set: { keyname: value }, }
Also, see MongoDB Compass - Modify Multiple Documents. This feature is introduced with Compass version 1.42.0 (released January 31, 2024). –
Pretence
© 2022 - 2024 — McMap. All rights reserved.