Skip to content

Commit bbc94d0

Browse files
author
Mohammad Hunan Chughtai
authored
(DOCSP-7604): update multiple docs (#22)
* (DOCSP-7604): iupdate many doc * updated link to update op * updated link to update op * updated link to update op * updated link to update op * added literal includes for code * updated verbiage * updated link * updated verbiage and code * updated code with additional options * Removed unneeded comment * updated verbiage * updated code comments * fixed grammar * removed upsert opt * removed upsert opt * removed upsert opt * removed upsert opt * fixed grammar * Removed unneeded line * removed additional opts
1 parent 797ffda commit bbc94d0

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ignored first line
2+
const { MongoClient } = require("mongodb");
3+
4+
// Replace the following with your MongoDB deployment's connection string.
5+
const uri =
6+
"mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
7+
8+
const client = new MongoClient(uri);
9+
10+
async function run() {
11+
try {
12+
await client.connect();
13+
14+
const database = client.db("sample_mflix");
15+
const collection = database.collection("movies");
16+
17+
// create a filter to update all movies with a 'G' rating
18+
const filter = { rated: "G" };
19+
20+
// increment every document matching the filter with 2 more comments
21+
const updateDoc = {
22+
$inc: {
23+
num_mflix_comments: 2,
24+
},
25+
};
26+
const result = await collection.updateMany(filter, updateDoc);
27+
console.log(result);
28+
} finally {
29+
await client.close();
30+
}
31+
}
32+
run().catch(console.dir);

source/usage-examples.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Usage Examples
1313
:doc:`findOne </usage-examples/findOne>`
1414
:doc:`insertMany </usage-examples/insertMany>`
1515
:doc:`updateOne</usage-examples/updateOne>`
16+
:doc:`updateMany</usage-examples/updateMany>`
1617
:doc:`replaceOne</usage-examples/replaceOne>`
1718
:doc:`distinct</usage-examples/distinct>`
1819
:doc:`command</usage-examples/command>`
@@ -30,6 +31,7 @@ Usage Examples
3031
/usage-examples/findOneAndUpdate
3132
/usage-examples/insertMany
3233
/usage-examples/updateOne
34+
/usage-examples/updateMany
3335
/usage-examples/replaceOne
3436
/usage-examples/distinct
3537
/usage-examples/command
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sasquatch?
22
duplicate key error explanation
3-
faq - diff b/w db.command({<crudMethod>}) and db.<crudMethod>
3+
bulk write operations
4+
faq - diff b/w db.command({<crudMethod>}) and db.<crudMethod>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=========================
2+
Update Multiple Documents
3+
=========================
4+
5+
.. default-domain:: mongodb
6+
7+
You can update multiple documents using the ``updateMany()`` method.
8+
``updateMany()`` accepts a filter object, and updates the documents
9+
that match the filter using a provided update document. The update
10+
document requires an :manual:`update operator
11+
</reference/operator/update>` to modify a field in a document.
12+
13+
You can define additional query options using the options object passed as the third parameter of the ``updateMany()`` method.
14+
15+
The ``updateMany()`` method returns a `Promise
16+
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>`_
17+
that resolves to an object. The ``modifiedCount`` field of this object
18+
shows how many documents were modified.
19+
20+
.. literalinclude:: /code-snippets/usage-examples/updateMany.js
21+
:language: javascript
22+
:linenos:

source/usage-examples/updateOne.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ You can update a single document using the `updateOne()
88
<https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#updateOne>`_
99
method. ``updateOne()`` accepts a filter document, and updates the first
1010
document that matches the filter using a provided update document. The
11-
update document requires an :manual:`Update
12-
Operator</reference/operator/update/#update-operators>`
13-
to modify a field in a document.
11+
update document requires an :manual:`Update Operator </reference/operator/update/#update-operators>` to modify a field in a document.
1412

1513
Create an `Object
1614
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>`_

0 commit comments

Comments
 (0)