Open In App

Mongoose Query.prototype.updateOne() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.updateOne() method of the Mongoose API is used on the Query objects. It allows us to update the document in the collection. MongoDB updates the first document that matches the filter regardless of the multi-option value. Let us understand updateOne() method using an example.

Syntax:

Model.updateOne( filter, update, options, callback );

Parameters: This method accepts four parameters as described below:

  • filter: It is used to specify filter conditions. It is in the form of an object.
  • update: It is used to specify the update object.
  • options: It is used to specify various properties in the form of the object.
  • callback: It is used to specify the callback function.

Return Value: This method returns the query object and updates the document with the latest values.

Setting up Node.js Mongoose Module:

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: The below example illustrates the basic functionality of the Mongoose Query updateOne() method, using then and catch block.

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = connectionObject.model('Student', studentSchema);
  
Student.updateOne(
    { name: 'Student3' }, { age: 33 }
).then(result => {
    console.log(result);
})


Step 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: The below example illustrates the basic functionality of the Mongoose Query updateOne() method, using asynchronous function and callback promise handling functionality.

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = 
    connectionObject.model('Student', studentSchema);
  
(async () => {
    Student.updateOne(
        { rollNumber: 176 }, { name: 'S2' }, 
        (error, result) => {
            if (error) {
                console.log('Error', error);
            } else {
                console.log('Result', result);
            }
        })
})();


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

node app.js

Output:

Result {
      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/query.html#query_Query-updateOne



Last Updated : 09 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads