Skip to content

Commit 63df9c3

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-24397 bulk write example (#1646) (#1652)
* DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example * DOCSP-24397-bulk-write-example Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent c8d59ef commit 63df9c3

File tree

4 files changed

+244
-283
lines changed

4 files changed

+244
-283
lines changed

source/core/bulk-write-operations.txt

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ perform bulk insert, update, and remove operations.
2525
MongoDB also supports bulk insert
2626
through the :method:`db.collection.insertMany()`.
2727

28+
.. _bulk-write-operations-ordered-vs-unordered:
29+
2830
Ordered vs Unordered Operations
2931
-------------------------------
3032

@@ -69,83 +71,17 @@ bulkWrite() Methods
6971
Each write operation is passed to :method:`~db.collection.bulkWrite()` as a
7072
document in an array.
7173

72-
For example, the following performs multiple write operations:
73-
74-
The ``characters`` collection contains the following documents:
75-
76-
.. code-block:: javascript
77-
78-
{ "_id" : 1, "char" : "Brisbane", "class" : "monk", "lvl" : 4 },
79-
{ "_id" : 2, "char" : "Eldon", "class" : "alchemist", "lvl" : 3 },
80-
{ "_id" : 3, "char" : "Meldane", "class" : "ranger", "lvl" : 3 }
81-
82-
The following :method:`~db.collection.bulkWrite()` performs multiple
83-
operations on the collection:
84-
85-
.. code-block:: javascript
86-
87-
try {
88-
db.characters.bulkWrite(
89-
[
90-
{ insertOne :
91-
{
92-
"document" :
93-
{
94-
"_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4
95-
}
96-
}
97-
},
98-
{ insertOne :
99-
{
100-
"document" :
101-
{
102-
"_id" : 5, "char" : "Taeln", "class" : "fighter", "lvl" : 3
103-
}
104-
}
105-
},
106-
{ updateOne :
107-
{
108-
"filter" : { "char" : "Eldon" },
109-
"update" : { $set : { "status" : "Critical Injury" } }
110-
}
111-
},
112-
{ deleteOne :
113-
{ "filter" : { "char" : "Brisbane" } }
114-
},
115-
{ replaceOne :
116-
{
117-
"filter" : { "char" : "Meldane" },
118-
"replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 }
119-
}
120-
}
121-
]
122-
);
123-
}
124-
catch (e) {
125-
print(e);
126-
}
127-
128-
The operation returns the following:
129-
130-
.. code-block:: javascript
131-
132-
{
133-
"acknowledged" : true,
134-
"deletedCount" : 1,
135-
"insertedCount" : 2,
136-
"matchedCount" : 2,
137-
"upsertedCount" : 0,
138-
"insertedIds" : {
139-
"0" : 4,
140-
"1" : 5
141-
},
142-
"upsertedIds" : {
143-
144-
}
145-
}
146-
147-
For more examples, see
148-
:ref:`bulkWrite() Examples <bulkwrite-example-bulk-write-operation>`
74+
Example
75+
-------
76+
77+
The example in this section uses the ``pizzas`` collection:
78+
79+
.. include:: /includes/pizza-example-collection.rst
80+
81+
.. include:: /includes/pizza-bulk-write-example.rst
82+
83+
For more examples, see :ref:`bulkWrite() Examples
84+
<bulkwrite-example-bulk-write-operation>`.
14985

15086
Strategies for Bulk Inserts to a Sharded Collection
15187
---------------------------------------------------
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
The following :method:`~db.collection.bulkWrite()` example runs
2+
these operations on the ``pizzas`` collection:
3+
4+
- Adds two documents using ``insertOne``.
5+
- Updates a document using ``updateOne``.
6+
- Deletes a document using ``deleteOne``.
7+
- Replaces a document using ``replaceOne``.
8+
9+
.. code-block:: javascript
10+
11+
try {
12+
db.pizzas.bulkWrite( [
13+
{ insertOne: { document: { _id: 3, type: "beef", size: "medium", price: 6 } } },
14+
{ insertOne: { document: { _id: 4, type: "sausage", size: "large", price: 10 } } },
15+
{ updateOne: {
16+
filter: { type: "cheese" },
17+
update: { $set: { price: 8 } }
18+
} },
19+
{ deleteOne: { filter: { type: "pepperoni"} } },
20+
{ replaceOne: {
21+
filter: { type: "vegan" },
22+
replacement: { type: "tofu", size: "small", price: 4 }
23+
} }
24+
] )
25+
} catch( error ) {
26+
print( error )
27+
}
28+
29+
Example output, which includes a summary of the completed operations:
30+
31+
.. code-block:: javascript
32+
:copyable: false
33+
34+
{
35+
acknowledged: true,
36+
insertedCount: 2,
37+
insertedIds: { '0': 3, '1': 4 },
38+
matchedCount: 2,
39+
modifiedCount: 2,
40+
deletedCount: 1,
41+
upsertedCount: 0,
42+
upsertedIds: {}
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. code-block:: javascript
2+
3+
db.pizzas.insertMany( [
4+
{ _id: 0, type: "pepperoni", size: "small", price: 4 },
5+
{ _id: 1, type: "cheese", size: "medium", price: 7 },
6+
{ _id: 2, type: "vegan", size: "large", price: 8 }
7+
] )

0 commit comments

Comments
 (0)