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
From Telemedicine to Wearables: Database Strain in the Future of Health
- 2 September 2025
- Software Engineering
Introduction The healthcare industry is experiencing a digital revolution. Telemedicine appointments, AI-powered diagnostics, and wearable health trackers are no longer futuristic ideas — they are everyday realities. But behind this rapid innovation lies a less visible challenge: the massive strain placed on healthcare databases. As the volume, velocity, and variety of medical data continue to … Continue reading “From Telemedicine to Wearables: Database Strain in the Future of Health”
How Enteros Combines AI SQL, AIOps, and Cloud FinOps in Its Observability Platform to Transform Cost Estimation and Database Performance in the Healthcare Sector
Introduction The healthcare sector is undergoing a profound digital transformation. From electronic health records (EHRs) and diagnostic imaging to AI-driven clinical decision support systems and telemedicine platforms, healthcare organizations are increasingly dependent on database performance, cloud resources, and real-time analytics to deliver reliable, efficient, and compliant care. However, this transformation comes with a cost. Healthcare … Continue reading “How Enteros Combines AI SQL, AIOps, and Cloud FinOps in Its Observability Platform to Transform Cost Estimation and Database Performance in the Healthcare Sector”
How Enteros Uses Root Cause Analysis and Data Lake Optimization to Boost RevOps Efficiency in the Gaming Sector
Introduction The gaming sector has grown into one of the most dynamic and data-intensive industries in the world. With billions of active players across mobile, console, and cloud-based platforms, gaming companies face enormous challenges in maintaining seamless performance, managing large-scale data, and optimizing revenue operations (RevOps). Data is the lifeblood of the gaming industry. From … Continue reading “How Enteros Uses Root Cause Analysis and Data Lake Optimization to Boost RevOps Efficiency in the Gaming Sector”
AI Workloads and Databases: Hidden Performance Risks That Slow Scaling
Introduction Artificial intelligence is rapidly moving from pilot projects to enterprise-scale operations. Companies in e-commerce, fintech, healthcare, and logistics are embedding AI into mission-critical workflows. These systems rely on massive volumes of real-time data to deliver accurate predictions and fast insights. But while most organizations focus on GPUs, cloud compute, and advanced algorithms, they often … Continue reading “AI Workloads and Databases: Hidden Performance Risks That Slow Scaling”