To update a value in MongoDB, you would use the `updateOne()` or `updateMany()` function, where `updateOne()` updates the first document that matches the query, and `updateMany()` updates all documents that match the query.
Here’s an example of how to update a document with `updateOne()` method in MongoDB:
```
const MongoClient = require(‘mongodb’).MongoClient;
const uri = “mongodb+srv://
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => { const collection = client.db(“test”).collection(“devices”);
// perform actions on the collection object var myquery = { name: “John” }; var newvalues = { $set: {name: “Mickey”, address: “Disneyland” } }; collection.updateOne(myquery, newvalues, function(err, res) { if (err) throw err; console.log(“1 document updated”); client.close(); }); }); ```In this case, we’re updating a single document where the `name` is “John”. We’re changing the `name` to “Mickey” and the `address` to “Disneyland”.
The `$set` operator replaces the value of a field with the specified value.
Make sure to replace `
For the `updateMany()` method, you can follow the same approach as the example above but change `updateOne()` to `updateMany()`. This will update all documents that match the criteria.
Remember to close the connection to the database when you’re done.