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
Driving Financial RevOps Efficiency with Enteros: AIOps-Powered Database Performance for Smarter Decision-Making
- 27 October 2025
- Database Performance Management
Introduction In today’s competitive financial ecosystem, where every second and every data point matters, achieving operational agility and revenue precision has become a top priority. Financial institutions — from banks and insurance companies to fintech innovators — are seeking more intelligent ways to manage their data-driven operations. The convergence of Revenue Operations (RevOps), database performance … Continue reading “Driving Financial RevOps Efficiency with Enteros: AIOps-Powered Database Performance for Smarter Decision-Making”
Revolutionizing Healthcare Efficiency with Enteros: SaaS Database Optimization, Generative AI, and Cloud FinOps Synergy
Introduction The healthcare sector has entered a new digital era driven by innovation, patient data analytics, and intelligent automation. From hospital management systems and patient monitoring to clinical research and predictive diagnostics, the industry’s growing dependence on cloud-based data systems has introduced both immense opportunities and operational challenges. As healthcare organizations scale their operations, managing … Continue reading “Revolutionizing Healthcare Efficiency with Enteros: SaaS Database Optimization, Generative AI, and Cloud FinOps Synergy”
⚡ The Hidden Blackout: How Data Lag Triggered a Grid Failure (and What It Taught Us About Energy Resilience)
📍 The Incident In late 2024, a regional utility company in Northern Europe experienced an unexpected power disruption.Not because of a storm.Not because of an overload.But because two monitoring systems were five seconds out of sync. Those five seconds cost the company 19 million euros in operational losses and emergency response. When analysts reconstructed the … Continue reading “⚡ The Hidden Blackout: How Data Lag Triggered a Grid Failure (and What It Taught Us About Energy Resilience)”
How Enteros is Revolutionizing Real Estate RevOps Through Generative AI and Smarter Database Performance
- 26 October 2025
- Database Performance Management
Introduction The real estate sector is no longer confined to property listings and offline transactions—it’s a complex, data-driven industry powered by technology, analytics, and automation. From property valuation and customer engagement to financial forecasting and deal execution, data plays a pivotal role in driving operational efficiency and revenue growth. However, managing this massive influx of … Continue reading “How Enteros is Revolutionizing Real Estate RevOps Through Generative AI and Smarter Database Performance”
