Skip to content

Commit c79f01c

Browse files
DOCSP-38414 Write Usage Examples (#70)
1 parent 537ad11 commit c79f01c

File tree

5 files changed

+220
-15
lines changed

5 files changed

+220
-15
lines changed

source/includes/usage-examples/sample-app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import pymongo
12
from pymongo import MongoClient
23

3-
uri = "<connection string URI>"
4-
client = MongoClient(uri)
5-
64
try:
5+
uri = "<connection string URI>"
6+
client = MongoClient(uri)
7+
78
database = client["<database name>"]
89
collection = database["<collection name>"]
910

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# start-insert-one
2+
result = collection.insert_one({ "<field name>" : "<value>" })
3+
4+
print(result.acknowledged)
5+
# end-insert-one
6+
7+
# start-insert-multiple
8+
document_list = [
9+
{ "<field name>" : "<value>" },
10+
{ "<field name>" : "<value>" }
11+
]
12+
13+
result = collection.insert_many(document_list)
14+
15+
print(result.acknowledged)
16+
# end-insert-multiple
17+
18+
# start-update-one
19+
query_filter = { "<field to match>" : "<value to match>" }
20+
update_operation = { "$set" :
21+
{ "<field name>" : "<value>" }
22+
}
23+
result = collection.update_one(query_filter, update_operation)
24+
25+
print(result.modified_count)
26+
# end-update-one
27+
28+
# start-update-multiple
29+
query_filter = { "<field to match>" : "<value to match>" }
30+
update_operation = { "$set" :
31+
{ "<field name>" : "<value>" }
32+
}
33+
result = collection.update_many(query_filter, update_operation)
34+
35+
print(result.modified_count)
36+
# end-update-multiple
37+
38+
# start-replace-one
39+
query_filter = { "<field to match>" : "<value to match>" }
40+
replace_document = { "<new document field name>" : "<new document value>" }
41+
42+
result = collection.replace_one(query_filter, replace_document)
43+
44+
print(result.modified_count)
45+
# end-replace-one
46+
47+
# start-delete-one
48+
query_filter = { "<field to match>" : "<value to match>" }
49+
50+
result = collection.delete_one(query_filter)
51+
52+
print(result.deleted_count)
53+
# end-delete-one
54+
55+
# start-delete-multiple
56+
query_filter = { "<field to match>" : "<value to match>" }
57+
58+
result = collection.delete_many(query_filter)
59+
60+
print(result.deleted_count)
61+
# end-delete-multiple
62+
63+
# start-bulk-write
64+
operations = [
65+
pymongo.InsertOne(
66+
{
67+
"<field name>" : "<value>"
68+
}
69+
),
70+
pymongo.UpdateMany(
71+
{ "<field to match>" : "<value to match>" },
72+
{ "$set" : { "<field name>" : "<value>" }},
73+
),
74+
pymongo.DeleteOne(
75+
{ "<field to match>" : "<value to match>" }
76+
),
77+
]
78+
79+
result = collection.bulk_write(operations)
80+
81+
print(result)
82+
# end-bulk-write

source/read.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ the relevant values for your MongoDB deployment.
5050

5151
.. include:: /includes/usage-examples/sample-app-intro.rst
5252

53-
.. literalinclude:: /includes/usage-examples/read-sample-app.py
53+
.. literalinclude:: /includes/usage-examples/sample-app.py
5454
:language: python
5555
:copyable:
5656
:linenos:
57-
:emphasize-lines: 10-12
57+
:emphasize-lines: 11-13
5858

5959
Find One
6060
--------

source/troubleshooting.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ frameworks.
166166
if __name__ == "__main__":
167167
app.run()
168168

169-
.. tip::
170-
171-
For more information, see the :ref:`querying-by-objectid` section in the Find guide.
172-
173169
.. _pymongo-fork-safe-details:
174170

175171
Why Does Forking a Process Cause a Deadlock?

source/write-operations.txt

Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44
Write Data to MongoDB
55
=====================
66

7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
717
.. meta::
818
:description: Learn how to use {+driver-short+} to write data to MongoDB.
19+
:keywords: usage examples, save, crud, create, code example
920

1021
.. toctree::
1122
:titlesonly:
@@ -18,9 +29,124 @@ Write Data to MongoDB
1829
/write/bulk-write
1930
/write/gridfs
2031

21-
- :ref:`pymongo-write-insert`
22-
- :ref:`pymongo-write-update`
23-
- :ref:`pymongo-write-replace`
24-
- :ref:`pymongo-write-delete`
25-
- :ref:`pymongo-bulk-write`
26-
- :ref:`pymongo-gridfs`
32+
Overview
33+
--------
34+
35+
On this page, you can see copyable code examples that show common
36+
methods you can use to write data to MongoDB with {+driver-short+}.
37+
38+
.. tip::
39+
40+
To learn more about any of the methods shown on this page, see the link
41+
provided in each section.
42+
43+
To use an example from this page, copy the code example into the
44+
:ref:`sample application <pymongo-write-sample>` or your own application.
45+
Be sure to replace all placeholders in the code examples, such as ``<connection string URI>``, with
46+
the relevant values for your MongoDB deployment.
47+
48+
.. _pymongo-write-sample:
49+
50+
.. include:: /includes/usage-examples/sample-app-intro.rst
51+
52+
.. literalinclude:: /includes/usage-examples/sample-app.py
53+
:language: python
54+
:copyable:
55+
:linenos:
56+
:emphasize-lines: 11-13
57+
58+
Insert One
59+
----------
60+
61+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
62+
:start-after: start-insert-one
63+
:end-before: end-insert-one
64+
:language: python
65+
:copyable:
66+
67+
To learn more about the ``insert_one()`` method, see the :ref:`Insert Documents
68+
<pymongo-write-insert>` guide.
69+
70+
Insert Multiple
71+
---------------
72+
73+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
74+
:start-after: start-insert-multiple
75+
:end-before: end-insert-multiple
76+
:language: python
77+
:copyable:
78+
79+
To learn more about the ``insert_many()`` method, see the :ref:`Insert Documents
80+
<pymongo-write-insert>` guide.
81+
82+
Update One
83+
----------
84+
85+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
86+
:start-after: start-update-one
87+
:end-before: end-update-one
88+
:language: python
89+
:copyable:
90+
91+
To learn more about the ``update_one()`` method, see the
92+
:ref:`Update Documents <pymongo-write-update>` guide.
93+
94+
Update Multiple
95+
---------------
96+
97+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
98+
:start-after: start-update-multiple
99+
:end-before: end-update-multiple
100+
:language: python
101+
:copyable:
102+
103+
To learn more about the ``update_many()`` method, see the
104+
:ref:`Update Documents <pymongo-write-update>` guide.
105+
106+
Replace One
107+
-----------
108+
109+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
110+
:start-after: start-replace-one
111+
:end-before: end-replace-one
112+
:language: python
113+
:copyable:
114+
115+
To learn more about the ``replace_one()`` method, see the
116+
:ref:`Replace Documents <pymongo-write-replace>` guide.
117+
118+
Delete One
119+
----------
120+
121+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
122+
:start-after: start-delete-one
123+
:end-before: end-delete-one
124+
:language: python
125+
:copyable:
126+
127+
To learn more about the ``delete_one()`` method, see the
128+
:ref:`Delete Documents <pymongo-write-delete>` guide.
129+
130+
Delete Multiple
131+
---------------
132+
133+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
134+
:start-after: start-delete-multiple
135+
:end-before: end-delete-multiple
136+
:language: python
137+
:copyable:
138+
139+
To learn more about the ``delete_many()`` method, see the
140+
:ref:`Delete Documents <pymongo-write-delete>` guide.
141+
142+
Bulk Write
143+
----------
144+
145+
.. literalinclude:: /includes/usage-examples/write-code-examples.py
146+
:start-after: start-bulk-write
147+
:end-before: end-bulk-write
148+
:language: python
149+
:copyable:
150+
151+
To learn more about the ``bulk_write()`` method, see the
152+
:ref:`Bulk Write <pymongo-bulk-write>` guide.

0 commit comments

Comments
 (0)