File tree Expand file tree Collapse file tree 3 files changed +68
-1
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 3 files changed +68
-1
lines changed Original file line number Diff line number Diff line change
1
+ // ignored first line
2
+ const { MongoClient } = require ( "mongodb" ) ;
3
+
4
+ const uri =
5
+ "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority" ;
6
+
7
+ const client = new MongoClient ( uri ) ;
8
+
9
+ async function run ( ) {
10
+ try {
11
+ await client . connect ( ) ;
12
+
13
+ const database = client . db ( "sample_mflix" ) ;
14
+ const collection = database . collection ( "movies" ) ;
15
+
16
+ // create a set of documents
17
+ const docOne = { name : "Red" , town : "Kanto" } ;
18
+ const docTwo = { name : "Blue" , town : "Kanto" } ;
19
+ const docThree = { name : "Leon" , town : "Galar" } ;
20
+ // create a docs array and add the documents to the array
21
+ const docs = [ docOne , docTwo , docThree ] ;
22
+ // specify an additional options object
23
+ const options = { } ;
24
+ options . bypassDocumentValidation = true ; // bypass document validation
25
+ options . ordered = true ; // prevent additional documents from being prevented if one fails
26
+ const result = await collection . insertMany ( docs , options ) ;
27
+ console . log ( `${ result . insertedCount } documents were inserted` ) ;
28
+ } finally {
29
+ await client . close ( ) ;
30
+ }
31
+ }
32
+ run ( ) . catch ( console . dir ) ;
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ Usage Examples
8
8
:doc:`find </usage-examples/find>`
9
9
:doc:`updateOne</usage-examples/updateOne>`
10
10
:doc:`deleteMany </usage-examples/deleteMany>`
11
+ :doc:`insertMany </usage-examples/insertMany>`
11
12
12
13
.. toctree::
13
14
:caption: Examples
@@ -16,4 +17,5 @@ Usage Examples
16
17
/usage-examples/find
17
18
/usage-examples/updateOne
18
19
/usage-examples/deleteMany
19
- /usage-examples/deleteOne
20
+ /usage-examples/deleteOne
21
+ /usage-examples/insertMany
Original file line number Diff line number Diff line change
1
+ =====================
2
+ Create Many Documents
3
+ =====================
4
+
5
+ .. default-domain:: mongodb
6
+
7
+ You can create multiple documents using the `insertMany()
8
+ <https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#insertMany>`_
9
+ method. The ``insertMany()`` takes an array of documents to insert into
10
+ the specified collection.
11
+
12
+ Create an `Object
13
+ <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>`_
14
+ to specify additional options. Set the ``bypassDocumentValidation``
15
+ field to bypass :manual:`schema validation <core/schema-validation/>`
16
+ rules in MongoDB 3.2 or higher. Specify ``ordered:true`` to prevent
17
+ inserting the remaining documents if the insertion failed for a previous
18
+ document in the array.
19
+
20
+ The ``insertMany()`` method returns a `Promise
21
+ <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>`_
22
+ that resolves to an object. The ``insertedIds`` field of this object is
23
+ an object that contains the ``_id`` of each inserted document. The
24
+ ``insertedCount`` field of this object is the number of documents
25
+ inserted into the collection.
26
+
27
+ Specifying incorrect parameters for your ``insertMany()`` operation can
28
+ cause problems. Attempting to insert a field to a value that would violate
29
+ unique index rules will throw a ``duplicate key error``.
30
+
31
+ .. literalinclude:: /code-snippets/usage-examples/insertMany.js
32
+ :language: javascript
33
+ :linenos:
You can’t perform that action at this time.
0 commit comments