Skip to content

Commit 14b9726

Browse files
read and write code examples (#73)
1 parent 11a2601 commit 14b9726

File tree

5 files changed

+115
-20
lines changed

5 files changed

+115
-20
lines changed

source/includes/read/distinct.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
# start-distinct
2+
results = restaurants.distinct("borough")
23

4+
for restaurant in results:
5+
print(restaurant)
36
# end-distinct
47

58
# start-distinct-with-query
9+
results = restaurants.distinct("borough", {
10+
"cuisine": "Italian"
11+
})
612

13+
for restaurant in results:
14+
print(restaurant)
715
# end-distinct-with-query
816

917
# start-distinct-with-comment
10-
18+
results = restaurants.distinct("name",
19+
{ "borough": "Bronx",
20+
"cuisine": "Pizza" },
21+
comment="Bronx pizza restaurants"
22+
)
1123
# end-distinct-with-comment

source/includes/write/bulk-write.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,80 @@
11
# start-bulk-insert-one
2-
2+
operation = pymongo.InsertOne(
3+
{
4+
"name": "Mongo's Deli",
5+
"cuisine": "Sandwiches",
6+
"borough": "Manhattan",
7+
"restaurant_id": "1234"
8+
}
9+
)
310
# end-bulk-insert-one
411

512
# start-bulk-update-one
6-
13+
operation = pymongo.UpdateOne(
14+
{ "name": "Mongo's Deli" },
15+
{ "$set": { "cuisine": "Sandwiches and Salads" }},
16+
)
717
# end-bulk-update-one
818

919
# start-bulk-update-many
10-
20+
operation = pymongo.UpdateMany(
21+
{ "name": "Mongo's Deli" },
22+
{ "$set": { "cuisine": "Sandwiches and Salads" }},
23+
)
1124
# end-bulk-update-many
1225

1326
# start-bulk-replace-one
14-
27+
operation = pymongo.ReplaceOne(
28+
{ "restaurant_id": "1234" },
29+
{
30+
"name": "Mongo's Pizza",
31+
"cuisine": "Pizza",
32+
"borough": "Brooklyn",
33+
"restaurant_id": "5678"
34+
}
35+
)
1536
# end-bulk-replace-one
1637

1738
# start-bulk-delete-one
18-
39+
operation = pymongo.DeleteOne({ "restaurant_id": "5678" })
1940
# end-bulk-delete-one
2041

2142
# start-bulk-delete-many
22-
43+
operation = pymongo.DeleteMany({ "name": "Mongo's Deli" })
2344
# end-bulk-delete-many
2445

2546
# start-bulk-write-mixed
47+
operations = [
48+
pymongo.InsertOne(
49+
{
50+
"name": "Mongo's Deli",
51+
"cuisine": "Sandwiches",
52+
"borough": "Manhattan",
53+
"restaurant_id": "1234"
54+
}
55+
),
56+
pymongo.InsertOne(
57+
{
58+
"name": "Mongo's Deli",
59+
"cuisine": "Sandwiches",
60+
"borough": "Brooklyn",
61+
"restaurant_id": "5678"
62+
}
63+
),
64+
pymongo.UpdateMany(
65+
{ "name": "Mongo's Deli" },
66+
{ "$set": { "cuisine": "Sandwiches and Salads" }},
67+
),
68+
pymongo.DeleteOne(
69+
{ "restaurant_id": "1234" }
70+
)
71+
]
2672

73+
results = restaurants.bulk_write(operations)
74+
75+
print(results)
2776
# end-bulk-write-mixed
2877

2978
# start-bulk-write-unordered
30-
79+
results = restaurants.bulk_write(operations, ordered=False)
3180
# end-bulk-write-unordered

source/includes/write/delete.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# start-delete-one
2+
query_filter = { "name": "Ready Penny Inn" }
23

4+
result = restaurants.delete_one(query_filter)
35
# end-delete-one
46

57
# start-delete-many
8+
query_filter = { "borough": "Brooklyn" }
69

10+
result = restaurants.delete_many(query_filter)
711
# end-delete-many
812

913
# start-delete-options
14+
query_filter = { 'name': {'$regex': 'Mongo' }}
1015

16+
result = restaurants.delete_many(query_filter, comment="Deleting Mongo restaurants")
1117
# end-delete-options

source/read/distinct.txt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ the ``restaurants`` collection:
5555

5656
.. output::
5757

58-
<placeholder output>
58+
Bronx
59+
Brooklyn
60+
Manhattan
61+
Missing
62+
Queens
63+
Staten Island
5964

6065
The results show every distinct value that appears in the ``borough`` field
6166
across all documents in the collection. Although several documents have the same
@@ -81,7 +86,11 @@ all documents that have a ``cuisine`` field value of ``"Italian"``:
8186

8287
.. output::
8388

84-
<placeholder output>
89+
Bronx
90+
Brooklyn
91+
Manhattan
92+
Queens
93+
Staten Island
8594

8695
Modify Distinct Behavior
8796
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -118,7 +127,8 @@ The following table describes the options you can set to customize
118127
- | An instance of ``Collation``.
119128

120129
The following example retrieves the distinct values of the ``name`` field for
121-
all documents that have a ``borough`` field value of ``"Bronx"``. It also uses
130+
all documents that have a ``borough`` field value of ``"Bronx"`` and a
131+
``cuisine`` field value of ``"Pizza"``. It also uses
122132
the ``comment`` option to add a comment to the operation.
123133

124134
.. io-code-block::
@@ -130,7 +140,12 @@ the ``comment`` option to add a comment to the operation.
130140

131141
.. output::
132142

133-
<placeholder output>
143+
$1.25 Pizza
144+
18 East Gunhill Pizza
145+
2 Bros
146+
Aenos Pizza
147+
Alitalia Pizza Restaurant
148+
...
134149

135150
API Documentation
136151
-----------------

source/write/bulk-write.txt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ Consider a scenario in which you want to insert a document into a collection,
2424
update multiple other documents, then delete a document. If you use
2525
individual methods, each operation requires its own database call. This guide
2626
shows you how to use bulk write operations to perform multiple write operations
27-
in a single database call.
27+
in a single database call.
28+
29+
Sample Data
30+
~~~~~~~~~~~
31+
32+
The examples in this guide use the ``sample_restaurants.restaurants`` collection
33+
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
34+
free MongoDB Atlas cluster and load the sample datasets, see the
35+
:ref:`<pymongo-get-started>` tutorial.
2836

2937
Define the Write Operations
3038
---------------------------
@@ -148,11 +156,16 @@ they're defined in the list.
148156
The following example performs multiple write operations by using the
149157
``bulk_write()`` method:
150158

151-
.. literalinclude:: /includes/write/bulk-write.py
152-
:start-after: start-bulk-write-mixed
153-
:end-before: end-bulk-write-mixed
154-
:language: python
155-
:copyable:
159+
.. io-code-block::
160+
161+
.. input:: /includes/write/bulk-write.py
162+
:start-after: start-bulk-write-mixed
163+
:end-before: end-bulk-write-mixed
164+
:language: python
165+
166+
.. output::
167+
168+
BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2, 'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []}, acknowledged=True)
156169

157170
If any of the write operations fail, {+driver-short+} raises a
158171
``BulkWriteError`` and does not perform any further operations.
@@ -212,7 +225,7 @@ the bulk write operation.
212225
</reference/command/delete/#std-label-delete-let-syntax>` in the
213226
MongoDB Server manual.
214227

215-
The following example uses the ``bulk_write()`` method with the ``ordered`` options set
228+
The following example calls the ``bulk_write()`` method from the preceding example, with the ``ordered`` option set
216229
to ``False``:
217230

218231
.. literalinclude:: /includes/write/bulk-write.py
@@ -221,7 +234,7 @@ to ``False``:
221234
:language: python
222235
:copyable:
223236

224-
If any of the write operations in the preceding example fail, {+driver-short+}
237+
If any of the write operations in an unordered bulk write fail, {+driver-short+}
225238
reports the errors only after attempting all operations.
226239

227240
.. note::

0 commit comments

Comments
 (0)