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
Maximizing SaaS Database Performance in the Financial Sector with AIOps and Cloud FinOps—Powered by Enteros
- 17 September 2025
- Database Performance Management
Introduction The financial sector is evolving rapidly in the era of digital-first services. Banks, investment firms, insurance providers, and fintech companies are managing unprecedented volumes of transactions, risk models, customer interactions, and compliance data. At the center of this transformation are SaaS databases, which power real-time trading platforms, digital banking, fraud detection systems, regulatory reporting, … Continue reading “Maximizing SaaS Database Performance in the Financial Sector with AIOps and Cloud FinOps—Powered by Enteros”
Optimizing Real Estate IT with AI SQL, Spot Instances, and Cloud Centers of Excellence—Powered by Enteros
Introduction The real estate sector is undergoing a digital revolution. From property search engines and virtual tours to predictive analytics for investment and AI-driven customer engagement, the industry is increasingly reliant on data-driven platforms. At the core of this transformation are databases—the backbone of property listings, mortgage systems, customer relationship management (CRM), IoT-enabled smart buildings, … Continue reading “Optimizing Real Estate IT with AI SQL, Spot Instances, and Cloud Centers of Excellence—Powered by Enteros”
Airline Check-ins Crashing: Passengers Stuck in Digital Queues
Introduction Air travel depends on speed and efficiency—but increasingly, passengers are delayed not at the gate, but in digital check-in queues. Database performance is at the heart of these failures. This article explains why airline IT systems struggle under pressure, the business risks involved, and how better database monitoring prevents costly meltdowns. Why Check-ins Depend … Continue reading “Airline Check-ins Crashing: Passengers Stuck in Digital Queues”
Smart Grids Crashing: Blackouts from DB Delays
Introduction Smart grids promise efficiency, sustainability, and resilience. But when databases powering them lag, the result isn’t just inconvenience—it’s regional blackouts and massive financial losses. In this article, we explore why database delays threaten energy infrastructure and how providers can protect against them. Why Databases Matter in Smart Grids Read moreMongoDB profiler and database performance … Continue reading “Smart Grids Crashing: Blackouts from DB Delays”