File tree Expand file tree Collapse file tree 5 files changed +59
-4
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 5 files changed +59
-4
lines changed Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11sasquatch?
22duplicate 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>
Original file line number Diff line number Diff line change 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:
Original file line number Diff line number Diff 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>`_
99method. ``updateOne()`` accepts a filter document, and updates the first
1010document 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
1513Create an `Object
1614<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>`_
You can’t perform that action at this time.
0 commit comments