nodejs
  1. nodejs-mongodb-update

Node.js MongoDB Update

In Node.js, you can update data in a MongoDB collection using the updateOne or updateMany method of the MongoDB driver. In this tutorial, we'll discuss how to update data in MongoDB using Node.js.

Syntax

Here's the syntax for updating data using the updateOne method:

db.collection('collectionName').updateOne(
   { filter },
   { $set: { update } }
)

Here's the syntax for updating data using the updateMany method:

db.collection('collectionName').updateMany(
   { filter },
   { $set: { update } }
)

The filter parameter is used to select the documents to update, and the update parameter is used to specify the changes to make to the selected documents.

Example

Let's say we have a users collection in a MongoDB database and we want to update the email address of a user with the ID of 5f919c4f28d5e147644c1a18. Here's how we can do it using the updateOne method:

const ObjectId = require('mongodb').ObjectID;

const filter = { _id: ObjectId("5f919c4f28d5e147644c1a18") };
const update = { $set: { email: "newemail@example.com" } };

db.collection('users').updateOne(filter, update, (err, res) => {
   if (err) throw err;
   console.log(`${res.modifiedCount} document updated`);
});

If we want to update the email address of all users with an age greater than or equal to 18, we can use the updateMany method:

const filter = { age: { $gte: 18 } };
const update = { $set: { email: "newemail@example.com" } };

db.collection('users').updateMany(filter, update, (err, res) => {
   if (err) throw err;
   console.log(`${res.modifiedCount} document(s) updated`);
});

Output

When we run the example code above, we'll get the number of documents updated in the console.

Explanation

In the example above, we used the updateOne method to update the email address of a user with the ID of 5f919c4f28d5e147644c1a18. We used the ObjectID constructor from the MongoDB driver to convert the ID string into an ObjectID instance that can be used in the filter.

We then used the updateMany method to update the email address of all users with an age greater than or equal to 18. We used the $set operator in the update parameter to specify the changes to make to the selected documents.

Use

You can use the updateOne or updateMany method of the MongoDB driver to update data in a MongoDB collection from your Node.js application.

Important Points

  • Remember to use the $set operator to specify the changes to make to the selected documents.
  • Use the filter parameter to select the documents to update based on certain criteria.
  • Always handle errors that may occur when updating data in MongoDB.

Summary

In this tutorial, we discussed how to update data in a MongoDB collection using Node.js. We covered the syntax, example, output, explanation, use, important points, and summary of updating data in MongoDB using Node.js. With this knowledge, you can now update data in a MongoDB collection from your Node.js application using the MongoDB driver.

Published on: