Table of contents
1.
Introduction
2.
Getting started
3.
Using save()
4.
Using Model.updateOne() and Model.updateMany()
5.
Using Document#updateOne()
6.
Using Model.findOneAndUpdate()
7.
 
8.
 
9.
 
10.
 
11.
Quick precap
12.
 
13.
 
14.
 
15.
 
16.
FAQs
17.
Key Takeaways:
Last Updated: Aug 13, 2025

Update the documents using Mongoose

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the previous articles, we have covered the need for mongoose and how we should proceed with writing documents there. Now, in this article, we are concerned about how we can update them. Sounds interesting, right !!.

Let’s watch in more detail.

Jennifer Aniston Yes GIF                                                          

 Source: giphy

Getting started

Any document is updated in four different ways in Mongoose:

  • Document#save
  • Model.updateOne() and updateMany()
  • Document#updateOne()
  • Model.findOneAndUpdate()

Now, if the work can be done in any of the ways, then what’s the need to have four of them?

Using save()

Consider the following example:

const schema = new mongoose.Schema({ name: String, title: String });
const CharacterModel = mongoose.model('Character', schema);

const doc = await CharacterModel.create({
  name: 'Coding Ninja,'
  title: `Online coding and learning platform`
});

// Document is updated by setting the property, and calling save().
doc.title = 'Hello Coders';
await doc.save();

Different nuances are present for the above example. save() is a method present in the example. This implies you must be having some files to save. You need to use find() or get() to identify or search any document. 

In the next steps, Mongoose knows how to change tracks. On calling doc.save() beneath the hood, Mongoose knows that you have already set the title, and it transforms your set call to the updateOne({ $set : { title } }). 

Using Model.updateOne() and Model.updateMany()

In general, we know that we need to load the data from the database for any kind of update in the data. But using Model.updateOne() and Model.updateMany(), data can be updated with loading it from the database. Go through the below example:

// Document update using `updateOne()`
await CharacterModel.updateOne({ name: 'James Gill' }, {
  title: 'Hello coders'
});

// Loading the document to see the updated value
const doc = await CharacterModel.findOne();
doc.title; // "Hello coders"

updateMany() plays quite a similar role. As the name suggests, updateOne() will update only one function at a time of the document, whereas, updateMany() will update every document that matches the filter and requirements. 

Try to use save() as much as you can. Rather using updateOne() or updateMany()

  • The work in updateOne() is automatic. If a document is loaded using find(), we can change it before the save().
  • Using updateOne(), you don’t need to load the document into memory, this feature is very helpful especially for huge size files.

Using Document#updateOne()

The Document#updateOne() function is very helpful for Model.updateOne(). It provides syntactic sugar syrup to it. If the document is already present in memory, doc.updateOne() will be structuring a Model.updateOne() call.

// Load the document
const doc = await CharacterModel.findOne({ name: 'james Gill' });

// Document is updated using `Document#updateOne()`
// Equivalent to `CharacterModel.updateOne({ _id: doc._id }, update)`
const update = { title: 'Hello coders' };
await doc.updateOne(update);

const updatedDoc = await CharacterModel.findOne({ name: 'James Gill' });
updatedDoc.title; // "Hello coders"

In general, the Document#updateOne() is rarely used. Its preferred to use save() and Model.updateOne() for cases when save() is not flexible. 

Using Model.findOneAndUpdate()

Model.findByIdAndUpdate() is another variation of Model.findOneAndUpdate() function. Both these functions behave similarly to the updateOne() function. The first document gets automatically updated when the first parameter matches. But in updateOne(), it returns back the updated document.

const doc = await CharacterModel.findOneAndUpdate(
  { name: 'James Gill' },
  { title: 'Helllo Coders' },
  // If `new` isn't true, `findOneAndUpdate()` will return the
  // document as it was _before_ it was updated.
  { newtrue }
);

doc.title; // "Hello coders"

 

 

 

 

Quick precap

  Atomic Doc in memory Returns updated doc Change tracking
Document#save()     ❌             ✅               ✅             ✅
Model.updateOne()     ✅             ❌               ❌             ❌
Model.updateMany()     ❌             ❌               ❌             ❌
Document#updateOne()     ❌             ✅               ❌             ❌
Model.findOneAndUpdate()     ✅             ❌               ✅             ❌

 

 

 

 

FAQs

  1. Define a schema?
    In Mongoose, the schema refers to the representation of a particular document, either partially or completely. In other ways, the way of expressing indexes, properties, values as well as constraints is known as schema. 
     
  2. Which is better to use MongoDB or mongoose?
    As such there is no benefit, both have their advantages based on different applications it is used for. For multi-tenant applications, MongoDB native driver is a good choice. Whereas mongoose as an ORM(Object Relational Mapping) is a better option for newbies.
     
  3. Mention the use of findOneAndDelete()?
    This function is preferred for finding out the matching documents, removing it, and then returning the modified document to the callback. 
     
  4. What value is returned by findOneAndDelete()?
    This function returns the modified document after the deletion has been performed(as a requirement). 

Key Takeaways:

This article covers various ways we can update various documents in Mongoose. It starts its discussion with the need for an update, then moves towards save(), Model.updateOne(), and many functions. At last, we have gone through the quick recap of the whole.

Keeping the theoretical knowledge at our fingertips helps us get about half the work done. To gain complete understanding, practice is a must. To achieve thorough knowledge, visit our page.

Live masterclass