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() {
21
21
const docs = [ docOne , docTwo , docThree ] ;
22
22
// specify an additional options object
23
23
const options = { } ;
24
- options . bypassDocumentValidation = true ; // bypass document validation
25
24
options . ordered = true ; // prevent additional documents from being prevented if one fails
26
25
const result = await collection . insertMany ( docs , options ) ;
27
26
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
6
6
7
7
:doc:`bulkWrite </usage-examples/bulkWrite>`
8
8
:doc:`deleteMany </usage-examples/deleteMany>`
9
+ :doc:`insertOne </usage-examples/insertOne>`
9
10
:doc:`deleteOne </usage-examples/deleteOne>`
10
11
:doc:`find </usage-examples/find>`
11
12
:doc:`findOne </usage-examples/findOne>`
12
13
:doc:`insertMany </usage-examples/insertMany>`
13
14
:doc:`updateOne</usage-examples/updateOne>`
14
15
15
-
16
16
.. toctree::
17
17
:caption: Examples
18
18
19
19
/usage-examples/bulkWrite
20
20
/usage-examples/deleteMany
21
21
/usage-examples/deleteOne
22
+ /usage-examples/insertOne
22
23
/usage-examples/find
23
24
/usage-examples/findOne
24
25
/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()
9
9
method. The ``insertMany()`` takes an array of documents to insert into
10
10
the specified collection.
11
11
12
- Create an `Object
12
+ You can create an `Object
13
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
14
+ to specify additional options. Specify ``ordered:true`` to prevent
17
15
inserting the remaining documents if the insertion failed for a previous
18
16
document in the array.
19
17
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