Open In App

Mongoose Document prototype.update() API

Improve
Improve
Like Article
Like
Save
Share
Report

The prototype.update() method of the Mongoose API can be used to update the document in a collection. It can be used on a document object present in the collection to update or modify its field values. This method will update the document and returns an object with various properties.

Syntax:

doc.update()

The prototype.update() method accepts three parameters:

  • update: It is an object array that contains key-value pairs where keys are the columns/attributes in the document.
  • options: It is an object with various properties.
  • callback: It is a callback function that will run once execution is completed.

Return Value: The prototype.update() function returns a promise. The result contains an object with various properties.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this:

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having two columns “name”, and “age”. In the end, we are using the update() method on the document object of the user model which will update the document field values with the values we will provide to the method as an object in the parameter. In this example, we have used an asynchronous function to find and update the document object.

  • app.js: Write down the below code in the app.js file:

App.js




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
  
const updateDoc = async () => {
  
    // Finding document object using doc _id
    const doc = await User.findById("63203694182cd3c22ea480ff");
  
    const output = await doc.update({ name: "User1 Updated" })
    console.log(output)
}
updateDoc();


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{
    acknowledged: true,
     modifiedCount: 1,  
     upsertedId: null,  
     upsertedCount: 0,  
     matchedCount: 1    
}

GUI Representation of the Database using Robo3T GUI tool:

 

Example 2: In this example, we are using the update() method on the document object of the User model to update the field values. To achieve update functionality in this example we have used promises.

  • app.js: Write down the below code in the app.js file:

App.js




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
  
// Finding document object using doc _id
const doc = User.findById("63204ad5182cd3c22ea486ae");
  
doc.update({ name: "User2 Updated", age: 200 })
    .then(output => {
        console.log(output);
});


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{
     acknowledged: true,
     modifiedCount: 1,  
     upsertedId: null,  
     upsertedCount: 0,  
     matchedCount: 1    
}

GUI Representation of the Database using Robo3T GUI tool:

 

Reference: https://mongoosejs.com/docs/api/document.html#document_Document-update



Last Updated : 27 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads