Mongoose findOneAndUpdate() Method (original) (raw)

Last Updated : 5 May, 2026

Mongoose findOneAndUpdate() is used to safely update a single document in MongoDB using an atomic operation. It helps maintain data consistency in multi-user environments by preventing race conditions and conflicting updates.

Atomic Operations in Mongoose

Mongoose supports atomic updates by executing MongoDB’s single-document atomic operations through its model methods (such as findOneAndUpdate()), ensuring safe updates in concurrent environments.

Syntax

Model.findOneAndUpdate(filter, update, options)

Examples of findOneAndUpdate() Method

The findOneAndUpdate() method is versatile and can be used in various scenarios.

Example 1: Basic Usage

Consider a scenario where we have a collection of users and we want to update the email address of a user named "Alen". Using findOneAndUpdate we can perform this operation atomically as defined below:

const mongoose = require('mongoose');

// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/GeeksforGeeks');

// User model
const User = mongoose.model('User', {
name: String,
email: String,
});

// Update email address for user "Alen"
User.findOneAndUpdate(
{ name: 'Alen' },
{ $set: { email: 'alen@example.com' } },
{ new: true },
(err, user) => {
if (err) return console.error(err);
console.log(user);
}
);

Example 2: Upsert with findOneAndUpdate()

In Mongoose, upsert updates a document if it exists or inserts a new one if no matching document is found. The findOneAndUpdate() method supports this using the upsert: true option.

Suppose we want to update the price of a book by title. If the book doesn’t exist, insert it.

const mongoose = require('mongoose');

// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/GeeksforGeeks');

// Book model
const Book = mongoose.model('Book', {
title: String,
price: Number,
});

// Update or insert a book with title
//"The Great Gatsby"
Book.findOneAndUpdate(
{ title: 'The Great Gatsby' },
{ $set: { price: 19.99 } },
{ upsert: true, new: true },
(err, book) => {
if (err) return console.error(err);
console.log(book);
}
);

Example 3: Updating Discriminator Keys

In Mongoose, discriminators let multiple document types exist in one collection. You can change a document’s type by updating its discriminator key using findOneAndUpdate().

const mongoose = require('mongoose');

// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/GeeksforGeeks');

// Base schema with discriminator key
const animalSchema = new mongoose.Schema(
{ name: String },
{ discriminatorKey: '__t' }
);

const Animal = mongoose.model('Animal', animalSchema);

// Update discriminator key to change animal type
Animal.findOneAndUpdate(
{ name: 'Fido', __t: 'Dog' },
{ $set: { __t: 'Cat' } },
{ new: true },
(err, animal) => {
if (err) return console.error(err);
console.log(animal);
}
);

Steps to Install Mongoose Module

Follow these steps to set up Mongoose in your project.

Step 1: Install Mongoose

You can visit the link to Install mongoose module. You can install this package by using this command.

npm install mongoose

This will add Mongoose to your project's dependencies.

Step 2: Verify Mongoose Installation

After installing mongoose module, you can check your mongoose version in command prompt using the command.

npm version mongoose

Step 3: Create and Set Up the Project

1. Create a new folder for your project (if you don’t already have one):

mkdir your-project-name
cd your-project-name

2. Create a new file, for example index.js, and add the following code to connect to MongoDB and use the findOneAndUpdate() method in Mongoose

**Filename: app.js

app.js `

const mongoose = require('mongoose');

async function run() { await mongoose.connect('mongodb://127.0.0.1:27017/GeeksforGeeks');

const userSchema = new mongoose.Schema({ name: String, age: Number }, { collection: 'student' });

const User = mongoose.model('User', userSchema);

const doc = await User.findOneAndUpdate( { age: { $gte: 5 } }, { $set: { name: "Denial" } }, { returnDocument: 'before' } );

console.log("Original Doc:", doc);

await mongoose.disconnect(); }

run().catch(console.error);

`

The project structure will look like this:

Screenshot-2026-04-24-112231

Below is the sample data in the database before the function is executed, you can use any GUI tool or terminal to see the database, like we have used MongoDB Compass tool as shown below:

Step 4: Running the Application

After creating the file and adding the code, run the application using the following command:

**Run:

node app.js

Screenshot-2026-02-07-114902

Step 5: Verify the Changes

You can verify the changes in your MongoDB database by using a GUI tool like Robo3T or MongoDB Compass to check if the document has been updated.

Screenshot-2026-02-07-114814