How can I add a new field to every document in an existent collection?

I know how to update an existing document's field but not how to add a new field to every document in a collection. How can I do this in the mongo shell?

Solution 1

Same as the updating existing collection field, $set will add a new fields if the specified field does not exist.

Check out this example:

{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
> item =
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
>{"_id" :ObjectId("4e93037bbf6f1dd3a0a9541a") },{$set : {"new_field":1}})
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "new_field" : 1, "test" : "a" }


In case you want to add a new_field to all your collection, you have to use empty selector, and set multi flag to true (last param) to update all the documents

  { $set: {"new_field": 1} },


In the above example last 2 fields false, true specifies the upsert and multi flags.

Upsert: If set to true, creates a new document when no document matches the query criteria.

Multi: If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.

This is for Mongo versions prior to 2.2. For latest versions the query is changed a bit

                          {$set : {"new_field":1}},

Solution 2

Since MongoDB version 3.2 you can use updateMany():

> db.yourCollection.updateMany({}, {$set:{"someField": "someValue"}})

Solution 3

To clarify, the syntax is as follows for MongoDB version 4.0.x:

db.collection.update({},{$set: {"new_field*":1}},false,true)

Here is a working example adding a published field to the articles collection and setting the field's value to true:

db.articles.update({},{$set: {"published":true}},false,true)

Solution 4

db.collection.updateMany({}, {$set: {"fieldName": ""}})

updateMany requires a matching condition for each document, since we are passing {} it is always true. And the second argument uses $set operator to add the required field in each document.

Solution 5

Pymongo 3.9+

update() is now deprecated and you should use replace_one(), update_one(), or update_many() instead.

In my case I used update_many() and it solved my issue:

db.your_collection.update_many({}, {"$set": {"new_field": "value"}}, upsert=False, array_filters=None)

Solution 6

if you are using mongoose try this,after mongoose connection

async ()=> await Mongoose.model("collectionName").updateMany({}, {$set: {newField: value}})

Solution 7

The answers above does't cover this scenario. I was looking for the similar query but want to add fields to few documents based on condition.

So, we can use first variable of updateMany to update fields only in few documents.

Example: I want to add a nullable field isDeprecated? to all those Users whose userType is Employer and has country "AAA".

db.users.updateMany({"userType": "Employer", "country": "AAA"}, {"$set": { "isDeprecated?": true }})  

This answer will be helpful in those scenarios as well, where we have to find the collection and then update. This can we done in single query like mentioned.