Open In App

Mongoose Document prototype.updateOne() API

Last Updated : 28 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The API prototype.updateOne() of the Mongoose API is used to update documents in a collection. This method can be used on mongoose documents and on that document you can update various fields in one command.

Syntax:

doc.updateOne()

Parameters: The prototype.updateOne() method accepts three parameters:

  • doc: It is an object with fields and values to be updated.
  • 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.updateOne() 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 or fields “name” and “age”. In the end, we are using the updateOne() method on the document object of the User model which will be updated once we execute the code. In this example, we have defined an asynchronous function named “findDoc” which will be responsible for finding the document in collection and updating the document using updateOne()  method on the document object. We are updating the name field of the User collection.

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

Javascript




// 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);
  
let doc;
const findDoc = async () => {
    doc = await User.findById("63203694182cd3c22ea480ff")
    const result = await doc.updateOne({ name: "User1 Update" })
    console.log(result)
}
findDoc()


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:  In this example, we have defined an asynchronous function named “findDoc” which will be responsible for finding the document in collection and updating the document using updateOne() method on the document object. We are updating the age field of the User collection.

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

Javascript




// 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);
  
let doc;
const findDoc = async () => {
    doc = await User.findById("63203694182cd3c22ea480ff")
    doc.updateOne({ age: 100 }).then(result => {
        console.log(result)
    })
}
findDoc()


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:

 

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



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

Similar Reads