Open In App

How to Add a New Field in a Collection

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Adding a new field to a MongoDB collection is a common task as applications grow and change. This process needs to be done carefully to ensure that existing data is not lost or corrupted. MongoDB provides several ways to add a new field while keeping our data safe and our application running smoothly.

In this article, We will learn about How to Add a New Field in a Collection by understanding various method along with methods practical examples and so on.

How to Add a New Field in a Collection?

In MongoDB, updating a collection to include a new field while maintaining existing data integrity is a common challenge. So, MongoDB offers several approaches to solve this issue efficiently. With the help of update operators such as $set, $unset, and $addFields, we can easily add new fields to our collection without affecting existing data or performance. Below is the method which helps us to Add a New Field in a Collection as follows:

  1. Using updateOne() Method with $set Operator
  2. Using updateMany() Method with $set Operators

Let’s set up an Environment:

To understand How to Add a New Field in a Collection we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called courses which contains information like title, fees, Instructor, and date of the courses in various documents.

coursescollection

courses

1. Using updateOne() Method with $set Operator

  • The updateOne() is the MongoDB method used to update a single document in a collection based on a filter condition.
  • The method accepts three parameters: a query filter, an update operation, and an optional update options document.

Example 1: Adding the new field [Duration] with value 30 days

The below query updates the Duration field to 30 days for the document with the ObjectId “6616b9f157bac1647326e11d” in the courses collection

db.courses.updateOne(
{ _id: ObjectId("6616b9f157bac1647326e11d") }, // Specify the document by its ObjectId
{ $set: { Duration: "30 days" } }
)

Output:

Using-updateOne01

duration

Example 2: Adding the new field [Student_enrolled] with value 67

The below query updates the Student_enrolled field to 67 for the document with the ObjectId “6616b9f157bac1647326e11d” in the courses collection

db.courses.updateOne(
{ _id: ObjectId("6616b9f157bac1647326e11d") }, // Specify the document by its ObjectId
{ $set: { "Student_enrolled": 67 } }
)

Output:

Using-updateOne02

studentenrolled

2. Using updateMany() Method with $set Operators

  • The updateMany() is the MongoDB method used to update multiple documents in a collection based on a filter condition.
  • The method accepts three parameters: a query filter, an update operation, and an optional update options document.

Example 1: Adding the new field [Course_mode] with value online

The below query updates the Course_mode field to “online” for all documents in the courses collection.

db.courses.updateMany(
{},
{ $set: { "Course_mode": "online" } }
)

Output:

Using-updateMany01

mode

Example 2: Adding the new field [Course_level] with value Basic

The below query updates the Course_level field to “Basic” for all documents in the courses collection.

db.courses.updateMany(
{},
{ $set: { "Course_level": "Basic" } }
)

Output:

Using-updateMany02

level

Conclusion

Overall, Adding a new field to a MongoDB collection is a simple process because of MongoDB’s update operators. Whether you need to add a single field or multiple fields, MongoDB offers efficient solutions that preserve data integrity and maintain application performance. By choosing the right approach, you can adapt your MongoDB schema to meet the needs of your application with easily.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads