Update Operations
The update operations are used to update or modify the existing document in the collection. You can perform update operations using the following methods provided by the MongoDB:
Method | Description |
---|---|
db.collection.updateOne() | It is used to update a single document in the collection that satisfy the given criteria. |
db.collection.updateMany() | It is used to update multiple documents in the collection that satisfy the given criteria. |
db.collection.replaceOne() | It is used to replace single document in the collection that satisfy the given criteria. |
Update Operations Example
Let’s look at some examples of the update operation from CRUD in MongoDB.
Example 1: In this example, we are updating the age of Sumit in the student collection using db.collection.updateOne() method. Example 2: In this example, we are updating the year of course in all the documents in the student collection using db.collection.updateMany() method.
MongoDB CRUD Operations
CRUD Operations (Create, Read, Update, and Delete) are the basic set of operations that allow users to interact with the MongoDB server.
As we know, to use MongoDB we need to interact with the MongoDB server to perform certain operations like entering new data into the application, updating data into the application, deleting data from the application, and reading the application data.
In this article, we will learn all 4 major operations– CREATE, READ, UPDATE, and DELETE that form the CRUD operations in MongoDB.