MongoDB Documentation

MongoDB Documentation – There are several methods to update Document in MongoDB:
- updateOne: refreshes one document that meets the filtering criteria and returns information about the update operation
- updateMany: refreshes all documents that meet the filtering criteria and returns information about the update operation
- findOneAndUpdate: refreshes one document that meets the filter criteria and returns an updated document.
findOneAndUpdate
The findOneAndUpdate() method updates one element. It accepts the following parameters:
- The criterion for filtering the document to be updated
- Update option
- Additional update options, which are null by default
- The callback function that is performed during an update
For example, let’s update the first user in the database who is 21 years old:
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/";
const mongoClient = new MongoClient(url, { useNewUrlParser: true });
let users = [{name: "Bob", age: 34} , {name: "Alice", age: 21}, {name: "Tom", age: 45}];
mongoClient.connect(function(err, client){
if(err) return console.log(err);
const db = client.db("usersdb");
const col = db.collection("usersdb");
col.insertMany(users, function(err, results){
col.findOneAndUpdate(
{age: 21}, // sampling criterion
{$set: {age: 25}}, // update parameter
function(err, result){
console.log(result);
client.close();
}
);
});
});
At first, 3 users shall be added to the database, and after the addition is updated.
The object { $set shall be used for updating: object {age: 25}}. The $set parameter shall update the values for a single field or group of fields. In this case, the age field shall be changed.
The third parameter, the callback function, displays the update result. By default, this is the old state of the modified document:

But, let’s say, after the update, we want to get not the old but the new state of the modified document. To do this, we can specify additional update options.
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/";
const mongoClient = new MongoClient(url, { useNewUrlParser: true });
mongoClient.connect(function(err, client){
if(err) return console.log(err);
const db = client.db("usersdb");
const col = db.collection("usersdb");
col.findOneAndUpdate(
{name: "Bob"}, // sampling criterion
{$set: {name: "Sam"}}, // update parameter
{ // additional update options
returnOriginal: false
},
function(err, result){
console.log(result);
client.close();
}
);
});
updateMany
The updateMany() method allows you to update all documents in the collection that meet the filtering criteria:
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/";
const mongoClient = new MongoClient(url, { useNewUrlParser: true });
mongoClient.connect(function(err, client){
if(err) return console.log(err);
const db = client.db("usersdb");
const col = db.collection("usersdb");
col.updateMany(
{name: "Sam"}, // filter criterion
{$set: {name: "Bob"}}, // update parameter
function(err, result){
console.log(result);
client.close();
}
);
});
updateOne
The updateOne() method is similar to the updateMany method except that it updates only one element. Unlike the findOneAndUpdate() method, it does not return a modified document:
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/";
const mongoClient = new MongoClient(url, { useNewUrlParser: true });
mongoClient.connect(function(err, client){
if(err) return console.log(err);
const db = client.db("usersdb");
const col = db.collection("usersdb");
col.updateOne(
{name: "Tom"},
{$set: {name: "Tom Junior", age:33}},
function(err, result){
console.log(result);
client.close();
}
);
});
Database, Collections, Documents: MongoDB
Enteros
About Enteros
IT organizations routinely spend days and weeks troubleshooting production database performance issues across multitudes of critical business systems. Fast and reliable resolution of database performance problems by Enteros enables businesses to generate and save millions of direct revenue, minimize waste of employees’ productivity, reduce the number of licenses, servers, and cloud resources and maximize the productivity of the application, database, and IT operations teams.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Growth-Ready eCommerce Operations: How Enteros Transforms Database Performance and Digital Scalability
- 24 December 2025
- Database Performance Management
Introduction The eCommerce sector is experiencing relentless growth driven by mobile commerce, omnichannel strategies, AI-powered personalization, global marketplaces, and real-time digital experiences. From flash sales and festive spikes to subscription commerce and cross-border transactions, modern eCommerce platforms operate at massive scale and velocity. At the core of this digital engine lies a complex ecosystem of … Continue reading “Growth-Ready eCommerce Operations: How Enteros Transforms Database Performance and Digital Scalability”
Optimizing Real Estate IT Economics: How Enteros Delivers Accurate Cost Estimation and Cost Attribution
Introduction The real estate sector is undergoing a fundamental digital transformation. From smart buildings and property management platforms to AI-driven valuation models, tenant experience apps, and real-time portfolio analytics, modern real estate enterprises are becoming data-intensive technology organizations. Behind every leasing platform, asset management system, CRM, IoT-enabled building dashboard, and analytics engine lies a complex … Continue reading “Optimizing Real Estate IT Economics: How Enteros Delivers Accurate Cost Estimation and Cost Attribution”
Scaling BFSI Innovation: How Enteros Aligns Performance Management, Cost Estimation, and Growth Strategy
- 23 December 2025
- Database Performance Management
Introduction The Banking, Financial Services, and Insurance (BFSI) sector is undergoing one of the most aggressive digital transformations in its history. Real-time payments, digital lending platforms, mobile banking apps, AI-driven fraud detection, open banking APIs, regulatory reporting systems, and wealth management platforms all rely on high-performing, always-available data infrastructure. At the heart of this digital … Continue reading “Scaling BFSI Innovation: How Enteros Aligns Performance Management, Cost Estimation, and Growth Strategy”
Healthcare IT Reinvented: How Enteros Delivers High-Performance Databases with Cloud FinOps Governance
Introduction Healthcare organizations are undergoing one of the most complex digital transformations of any industry. Electronic Health Records (EHRs), telemedicine platforms, clinical research systems, patient engagement apps, AI-assisted diagnostics, and revenue cycle management tools all rely on high-performing, always-available databases running across cloud and hybrid infrastructures. However, as healthcare IT ecosystems expand, so do the … Continue reading “Healthcare IT Reinvented: How Enteros Delivers High-Performance Databases with Cloud FinOps Governance”
