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
Optimizing Database Performance with Enteros: Leveraging Cloud FinOps and Observability for the Financial Sector
- 23 December 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Enteros: Enhancing Database Security Cost Management with RevOps and AIOps for the Insurance Sector
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Enteros: Revolutionizing Database Performance with AIOps, RevOps, and DevOps for the Insurance Sector
- 20 December 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Enteros: Transforming Database Software with Cloud FinOps for the Technology Sector
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…