Collections and schema

If I have a collection of documents, can I use the schema to add a field to all the documents in that collection at once?


Hey @Sarah_39095
Short answer no. And more can be found here

There really is no schema to update in mongodb. It has a flexible data model which will allow you to add any field of any type to a document. There are no restrictions with the model(“schema”).

Therefore if you wanted to add a new field, value pair to each document in a collection, you would run an db.collection.updateMany({}, {fieldToUpdate: newValue, ...})

To Add from the docs:

In practice, however, the documents in a collection share a similar structure

And you could look at Schema Validations.

Welcome and best of luck in the rest of the course(s)! :smile:

1 Like