How to add a new field to all documents in the MongoDB collection? In this article, you will learn to create a new field for each document in a collection. It can be done efficiently with the updateMany()
method and the appropriate update operator.
Let’s consider a student
collection and add a new field to the document.
# Create student collection
db.student.insertMany([
{
"_id": 1,
"FirstName": "Jimmy",
"LastName": "Neutron",
"Marks1": 20,
"Marks2": 18
},
{
"_id": 2,
"Name": "Peter",
"LastName": "Ian",
"Marks1": 17,
"Marks2": 19
}
]
)
1. Using $set Operator to Add New Field
You can use the updateMany() method to update a document and use the $set operator along with it to add a new field to all documents in a MongoDB collection.
For example, we have a MongoDB query below where the updateMany()
method is used to update multiple documents in the collection. The first argument, which corresponds to every document in the collection, is the empty query. The second argument is a $set
operator which adds a new field Degree
with value Computer Science
for every document.
# Usage of updateMany() method
db.student.updateMany({}, { $set: { Degree: "computer science" } });
This output assures that the documents are modified with the field Degree
and the value set to it.
Now, we have used the find()
query to see the updated documents with the new field here.
You can also add a new field with a dynamic value based on the current date with the $set
operator. For example, we have given a query where the $set
operator takes the new field Enrollment
and set the value of this new field with the new Date()
function which will set its value to the current date and time.
# Add new filed with current date & time
db.student.updateMany({}, {
$set: { Enrollment: new Date() }
});
The output of the above query ensures that documents are updated with the new fields.
The output of the modified documents displayed the Enrollment
with the current date and time below.
More details about the $set operator can be found here.
2. Add New Field based on Other Fields
In order to add additional new fields to all documents by deriving from existing fields, we can also use the $add
operator in MongoDB. This operator is employed using an aggregation framework. The $add
operators conduct the operation of the addition of the number fields.
For example, we have used the $set
operator in the following query which is specified with the new field TotalMarks
that holds the results of the $add
operator. The new field TotalMarks
is set with the $add
operator that will add the existing fields Marks1
and Marks2
.
# Usage of $add operator
db.student.updateMany({}, {
$set: { TotalMarks: { $add: ["$Marks1", "$Marks2"] } }
});
The documents generated the new field TotalMarks
with the resultant values of Marks1
and Marks2
.
3. Using the forEach() method to add a new field in MongoDB
Alternatively, you can add an additional field to each document by iterating through the group of documents using the MongoDB forEach()
method. For example in the following query, we used the forEach()
method to loop through every document in the collection, and for each document, it will add a new field called Semester
with the value 4th
.
# Usage of forEach() method
db.student.find().forEach(function(doc) {
db.student.updateMany({_id: doc._id}, {$set: {Semester: "4th"}});
});
The output is updated with the new field Semester
and the value specified to it.
4. Using the $addFields operator to add a new field in MongoDB
Furthermore, a MongoDB collection’s documents can all have new fields added to them using the aggregation process. With this approach, each document will get a new field via the $addFields
pipeline step. In the following query, we added a new field called shift
with the value morning
to every document in the collection by using the $addFields
operator, and then replaced the existing collection student
with the modified version.
#Usage of $addFields operator
db.student.aggregate([
{$addFields: {Shift: "morning"}},
{$out: "student"}
]);
The output here displayed that every document is modified with the new field Shift
with the value morning
.
5. Conclusion
In this article, you have learned several methods of how to add a new field to all documents of the MongoDB collection for example, you can use the updateMany()
method and the appropriate update operator. The method we choose will depend on our specific use case and our data structure.