Open In App

Mongoose Queries Model.updateOne() Function

Last Updated : 09 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Model.updateOne() function in Mongoose is a powerful method used to update a single document within a MongoDB collection. It allows you to specify the conditions for selecting a document and then provides a way to apply updates to that document. In this article, we will explore how updateOne() works, its syntax, practical examples, and use cases to efficiently update documents in MongoDB using Mongoose.

What is Mongoose Model.updateOne()?

The updateOne() method in Mongoose allows you to update the first document that matches the filter conditions provided. It is ideal for updating a single document at a time and is often used in situations where only one document needs modification, such as updating a user’s email or changing a product's price.

Syntax:

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

Parameters:

  • filter: It is a mongoose object which identifies the existing document to update.
  • update: It is a mongoose object which is the document that will update the data in the existing document.
  • options: It is an optional mongoose object which is derived from Query.prototype.setOptions().
  • callback: It is a callback function that accepts 2 parameters: error and writeOpResult.

Return type

  • The method returns a Query object, which allows for further chaining.

Setting up Node.js Mongoose Module

Step 1: Create a new Node.js application

npm init

Step 2: Install the required module using the following command

npm install mongoose

Step 3: Project Structure

mongoose-update-example/
├── node_modules/
├── package.json
└── app.js

Example 1: Update Document by Field Value

In this example, we will demonstrate how to use the updateOne() method to update an existing document that has the age "19".

Filename: app.js

const mongoose = require('mongoose')

// Database connection
mongoose.connect('mongodb://localhost:27017/query-helpers',
{
dbName: 'event_db',
useNewUrlParser: true,
useUnifiedTopology: true
}, err => err ? console.log(err)
: console.log('Connected to database'));

const personSchema = new mongoose.Schema({
name: {
type: String,
select: false
},
age: {
type: Number,
}
});

const personsArray = [
{
name: 'Luffy',
age: 19
},
{
name: 'Nami',
age: 30
},
{
name: 'Zoro',
age: 35
}
]

const Person = mongoose.model('Person', personSchema);

(async () => {
await Person.insertMany(personsArray);
const res = await Person.updateOne({ age: 19 }, { age: 23 });
console.log(res.modifiedCount);
})()

Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output:

GUI Representation of the Database using MongoDB Compass:

Explanation: In this example, we connect to the database, insert sample documents, and then use updateOne() to update the document where the age field is 19. The result confirms that one document was updated.

Example 2: Update Document by Name

In this example, we will use this method to update an existing document where the name is "Luffy" to change it to "Shanks".

Filename: app.js

const mongoose = require('mongoose')

// Database connection
mongoose.connect('mongodb://localhost:27017/query-helpers',
{
dbName: 'event_db',
useNewUrlParser: true,
useUnifiedTopology: true
}, err => err ? console.log(err)
: console.log('Connected to database'));

const personSchema = new mongoose.Schema({
name: {
type: String,
select: false
},
age: {
type: Number,
}
});

const personsArray = [
{
name: 'Luffy',
age: 19
},
{
name: 'Nami',
age: 30
},
{
name: 'Zoro',
age: 35
}
]

const Person = mongoose.model('Person', personSchema);

(async () => {
await Person.insertMany(personsArray);
const res = await Person.updateOne(
{ name: 'Luffy' }, { name: 'Shanks' });
console.log(res.modifiedCount);
})()

Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output:

GUI Representation of the  Database using MongoDB Compass:

Explanation: This example demonstrates how to update a document with the name "Luffy" and change it to "Shanks." The updateOne() method successfully updates the document, and the output confirms the modification.

Conclusion

The Mongoose updateOne() method is an efficient way to update a single document in a MongoDB collection. By understanding the syntax, parameters, and examples provided in this guide, you can confidently use updateOne() to perform targeted updates in your applications. Whether you are updating user details, modifying product information, or altering other types of data, this method offers a simple and effective approach to making changes to your database


Next Article

Similar Reads