Skip to content

Commit e9d1127

Browse files
authored
DOCSP-7602: adds bulkWrite usage example (#11)
* DOCSP-7602: adds bulkWrite usage example * DOCSP-7602: standardize code & prose with other examples * DOCSP-7602: it helps to save before committing * cleaning up errors * DOCSP-7602: wordsmithing
1 parent a3dcbfd commit e9d1127

File tree

6 files changed

+165
-7
lines changed

6 files changed

+165
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ build/
55
.#*
66
*.bak
77
giza.log
8+
.vscode*

conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
pygments_style = 'sphinx'
6262

6363
extlinks = {
64-
'manual': ('http://docs.mongodb.com/manual%s', ''),
64+
'manual': ('https://docs.mongodb.com/manual%s', ''),
6565
}
6666

6767
## add `extlinks` for each published version.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { MongoClient } = require("mongodb");
2+
const fs = require("fs");
3+
4+
// Replace the following with your MongoDB deployment's connection
5+
// string.
6+
const uri =
7+
"mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
8+
9+
const client = new MongoClient(uri, {
10+
useNewUrlParser: true,
11+
useUnifiedTopology: true
12+
});
13+
14+
async function run() {
15+
try {
16+
await client.connect();
17+
18+
const mflix = client.db("sample_mflix");
19+
20+
// Replace with the path to your copy of the users.json file
21+
var users = JSON.parse(fs.readFileSync("<path/to/users.json/file"));
22+
23+
var usersToInsert = [];
24+
25+
// Loop through each user in the users array and format the data from
26+
// the JSON file to be a series of insertOne operations. Append
27+
// each appropriately formatted object to the usersToInsert array.
28+
users.forEach(user => {
29+
usersToInsert.push({ insertOne: { document: user } });
30+
});
31+
32+
// Perform the bulk write operation and print out the number
33+
// of documents inserted and/or any error messages.
34+
// This is a routine data import
35+
// that does not rely on the ordering of the operations to ensure
36+
// data consistency, so you can set the ordered option to false
37+
// to improve write performance.
38+
mflix
39+
.collection("users")
40+
.bulkWrite(usersToInsert, { ordered: false }, function(error, result) {
41+
// The bulk write operation may successfully insert documents
42+
// and also return an error, so both cases must be handled.
43+
if (result.nInserted > 0) {
44+
console.log("Number of documents inserted: " + result.nInserted);
45+
} else {
46+
console.log(
47+
"No documents were inserted during the bulk write operation"
48+
);
49+
}
50+
51+
if (error) {
52+
console.log("Error: " + error);
53+
}
54+
});
55+
} finally {
56+
await client.close();
57+
}
58+
}
59+
run().catch(console.dir);

source/usage-examples.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ Usage Examples
44

55
.. default-domain:: mongodb
66

7-
:doc:`findOne </usage-examples/findOne>`
8-
:doc:`find </usage-examples/find>`
9-
:doc:`updateOne</usage-examples/updateOne>`
7+
:doc:`bulkWrite </usage-examples/bulkWrite>`
108
:doc:`deleteMany </usage-examples/deleteMany>`
9+
:doc:`deleteOne </usage-examples/deleteOne>`
10+
:doc:`find </usage-examples/find>`
11+
:doc:`findOne </usage-examples/findOne>`
1112
:doc:`insertMany </usage-examples/insertMany>`
13+
:doc:`updateOne</usage-examples/updateOne>`
14+
1215

1316
.. toctree::
1417
:caption: Examples
1518

16-
/usage-examples/findOne
17-
/usage-examples/find
18-
/usage-examples/updateOne
19+
/usage-examples/bulkWrite
1920
/usage-examples/deleteMany
2021
/usage-examples/deleteOne
22+
/usage-examples/find
23+
/usage-examples/findOne
24+
/usage-examples/findOneAndUpdate
2125
/usage-examples/insertMany
26+
/usage-examples/updateOne

source/usage-examples/.gitkeep

Whitespace-only changes.

source/usage-examples/bulkWrite.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
=======================
2+
Perform Bulk Operations
3+
=======================
4+
5+
.. default-domain:: mongodb
6+
7+
Overview
8+
--------
9+
10+
:node-api:`collection.bulkWrite <Collection.html#bulkWrite>` lets you
11+
perform bulk write operations against a *single* collection. With
12+
``collection.bulkWrite()``, you specify a list of operations to perform and
13+
MongoDB then executes those operations in bulk. Bulk write supports
14+
``insertOne``, ``updateOne``, ``updateMany``, ``deleteOne``,
15+
``deleteMany``, and ``replaceOne`` operations. Refer to the method
16+
documentation for full details.
17+
18+
``bulkWrite()`` accepts the following parameters:
19+
20+
- ``operations``: specifies the bulk operations to
21+
perform. Each operation is passed to ``bulkWrite()`` as an object in
22+
an array.
23+
24+
- ``options``: *optional* settings that affect the execution
25+
of the operation, such as :manual:`write concern
26+
</reference/write-concern>` and order.
27+
28+
By default, MongoDB executes bulk operations one-by-one in the
29+
specified order (i.e. serially). During an ordered bulk write, if
30+
an error occurs during the processing of an operation, MongoDB returns without
31+
processing the remaining operations in the list. In contrast,
32+
when ``ordered`` is ``false``, MongoDB continues to process remaining
33+
write operations in the list. Unordered operations are faster as
34+
MongoDB can execute the operations in parallel, but the results of the
35+
operation may vary. For example, a ``deleteOne`` operation run before
36+
an ``updateMany`` operation might have a different result from running
37+
it after the ``updateMany``. Refer to :manual:`Execution of Operations
38+
</reference/method/db.collection.bulkWrite/#execution-of-operations>` for more
39+
information.
40+
41+
- ``callback``: command result callback. Like other collection methods,
42+
``bulkWrite()`` returns a Promise if you do not specify a callback. The
43+
Promise resolves to a :node-api:`bulkWriteOpCallback </Collection.html#~bulkWriteOpCallback>` object containing the
44+
result of the operation.
45+
46+
``bulkWrite()`` returns a ``BulkWriteResult`` object
47+
that includes the number of updated or inserted documents, the ``_id`` of
48+
each document, and any ``writeError`` or ``writeConcernErrors``. Refer to
49+
the documentation for :node-api:`BulkWriteResult </BulkWriteResult>` for
50+
more information.
51+
52+
If you create an index with a :manual:`unique constraint
53+
</core/index-unique>`, you might encounter a duplicate key write error
54+
during an operation. The following example shows a duplicate key error
55+
encountered when two of the users in the inserted sample dataset had the
56+
same email address.
57+
58+
.. code-block:: sh
59+
60+
Error during bulkWrite, BulkWriteError: E11000 duplicate key error collection: sample_mflix.users index: email_1 dup key: { : "[email protected]" }
61+
62+
Similarly, if you attempt to perform a bulk write against a collection
63+
that uses :manual:`schema validation </core/schema-validation>`, you may
64+
encounter warnings or errors related to the formatting of inserted or modified
65+
documents.
66+
67+
Example
68+
-------
69+
70+
The following code sample performs a bulk write operation against the
71+
``users`` collection in the ``sample_mflix`` database. Specifically, the
72+
program formats the data from a ``users.json`` source file to perform a
73+
series of ``insertOne`` operations with the ``bulkWrite()`` collection
74+
method. Download the dataset here: `users.json
75+
<https://raw.githubusercontent.com/mongodb-university/universal-driver-examples/master/users.json>`_.
76+
77+
Documents in the ``users`` collection have ``email``, ``password``,
78+
and ``name`` fields, as well as the generated ``_id`` field.
79+
``users.json`` contains an array of objects that map directly to the
80+
existing fields in the collection, as in the following:
81+
82+
.. code-block:: javascript
83+
84+
{
85+
"email" : "[email protected]",
86+
"password" : "450f6704710dcf8033157978f7fbdfde",
87+
"name" : "Marie Conrad"
88+
}
89+
90+
.. literalinclude:: /code-snippets/usage-examples/bulkWrite-example.js
91+
:language: javascript
92+
93+

0 commit comments

Comments
 (0)