Skip to content

Commit 8179b3a

Browse files
author
Mohammad Hunan Chughtai
authored
(DOCSP-7600): insertOne usage example(#17)
* (DOCSP-7600): insertOne init * fixed pr to address comments * removed unneeded additional options * removed bypassschema from insertmany because we dont want that option * fixed wording * updated wording
1 parent e9d1127 commit 8179b3a

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

source/code-snippets/usage-examples/insertMany.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff 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`);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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);

source/usage-examples.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

source/usage-examples/insertMany.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ You can create multiple documents using the `insertMany()
99
method. The ``insertMany()`` takes an array of documents to insert into
1010
the 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
1715
inserting the remaining documents if the insertion failed for a previous
1816
document in the array.
1917

source/usage-examples/insertOne.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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:

0 commit comments

Comments
 (0)