File tree Expand file tree Collapse file tree 5 files changed +51
-6
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 5 files changed +51
-6
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,6 @@ async function run() {
2121 const docs = [ docOne , docTwo , docThree ] ;
2222 // specify an additional options object
2323 const options = { } ;
24- options . bypassDocumentValidation = true ; // bypass document validation
2524 options . ordered = true ; // prevent additional documents from being prevented if one fails
2625 const result = await collection . insertMany ( docs , options ) ;
2726 console . log ( `${ result . insertedCount } documents were inserted` ) ;
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+ // create a document object
16+ const doc = { name : "Red" , town : "kanto" } ;
17+ const result = await collection . insertOne ( doc ) ;
18+
19+ console . log (
20+ `${ result . insertedCount } documents were inserted with the _id: ${ result . insertedId } ` ,
21+ ) ;
22+ } finally {
23+ await client . close ( ) ;
24+ }
25+ }
26+ run ( ) . catch ( console . dir ) ;
Original file line number Diff line number Diff line change @@ -6,19 +6,20 @@ Usage Examples
66
77:doc:`bulkWrite </usage-examples/bulkWrite>`
88:doc:`deleteMany </usage-examples/deleteMany>`
9+ :doc:`insertOne </usage-examples/insertOne>`
910:doc:`deleteOne </usage-examples/deleteOne>`
1011:doc:`find </usage-examples/find>`
1112:doc:`findOne </usage-examples/findOne>`
1213:doc:`insertMany </usage-examples/insertMany>`
1314:doc:`updateOne</usage-examples/updateOne>`
1415
15-
1616.. toctree::
1717 :caption: Examples
1818
1919 /usage-examples/bulkWrite
2020 /usage-examples/deleteMany
2121 /usage-examples/deleteOne
22+ /usage-examples/insertOne
2223 /usage-examples/find
2324 /usage-examples/findOne
2425 /usage-examples/findOneAndUpdate
Original file line number Diff line number Diff line change @@ -9,11 +9,9 @@ You can create multiple documents using the `insertMany()
99method. The ``insertMany()`` takes an array of documents to insert into
1010the specified collection.
1111
12- Create an `Object
12+ You can create an `Object
1313<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
14+ to specify additional options. Specify ``ordered:true`` to prevent
1715inserting the remaining documents if the insertion failed for a previous
1816document in the array.
1917
Original file line number Diff line number Diff line change 1+ =================
2+ Create a Document
3+ =================
4+
5+ .. default-domain:: mongodb
6+
7+ To create a document, define an object variable with the fields and values that you want to specify. You can insert this document into a collection using the `insertOne()
8+ <https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#insertOne>`_
9+ method. If the specified collection does not exist, the
10+ ``insertOne()`` method creates the collection.
11+
12+ The ``insertOne()`` method returns a `Promise
13+ <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>`_
14+ that resolves to an object. The ``insertedId`` field of this object is
15+ the ``_id`` of the inserted document. The ``insertedCount`` field of
16+ this object has a value of 0 if a document was not created, and a value
17+ of 1 if a document was created.
18+
19+ .. literalinclude:: /code-snippets/usage-examples/insertOne.js
20+ :language: javascript
21+ :linenos:
You can’t perform that action at this time.
0 commit comments