How to use updateOne() Method with $set Operator In Databases

  • 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:

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:

studentenrolled

How to Add a New Field in a Collection

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.

Similar Reads

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

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....

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....

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....