Skip to content

Commit 0f8ac56

Browse files
DOCSP-38413 Read Usage Examples (#68)
1 parent e26c4d2 commit 0f8ac56

File tree

6 files changed

+172
-17
lines changed

6 files changed

+172
-17
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# start sample app
2+
from pymongo import MongoClient
3+
4+
try:
5+
# start authentication code here
6+
7+
# end authentication code here
8+
9+
client.admin.command("ping")
10+
print("Authenticated successfully")
11+
12+
# other application code
13+
14+
client.close()
15+
16+
except Exception as e:
17+
raise Exception(
18+
"The following error occurred: ", e)
19+
# end sample app
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# start-find-one
2+
results = collection.find_one({ "<field name>" : "<value>" })
3+
4+
print(results)
5+
# end-find-one
6+
# start-find
7+
results = collection.find({ "<field name>" : "<value>" })
8+
9+
for document in results:
10+
print(document)
11+
# end-find
12+
# start-count-all
13+
count = collection.count_documents()
14+
15+
print(count)
16+
#end-count-all
17+
# start-count-query
18+
count = collection.count_documents({ "<field name>": "<value>" })
19+
20+
print(count)
21+
# end-count-query
22+
23+
# start-estimated-count
24+
count = collection.estimated_document_count()
25+
26+
print(count)
27+
# end-estimated-count
28+
# start-distinct
29+
results = collection.distinct("<field name>")
30+
31+
for document in results:
32+
print(document)
33+
# end-distinct
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
# start sample app
21
from pymongo import MongoClient
32

4-
try:
5-
# start authentication code here
3+
uri = "<connection string URI>"
4+
client = MongoClient(uri)
65

7-
# end authentication code here
6+
try:
7+
database = client["<database name>"]
8+
collection = database["<collection name>"]
89

9-
client.admin.command("ping")
10-
print("Authenticated successfully")
10+
# start example code here
1111

12-
# other application code
12+
# end example code here
1313

1414
client.close()
1515

1616
except Exception as e:
1717
raise Exception(
1818
"The following error occurred: ", e)
19-
# end sample app

source/read.txt

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44
Read Data from 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::
8-
:description: Learn how to use (+driver-short+} to read data from MongoDB.
18+
:description: Learn how to use {+driver-short+} to read data from MongoDB.
19+
:keywords: usage examples, query, find, code example
920

1021
.. toctree::
1122
:titlesonly:
@@ -19,11 +30,100 @@ Read Data from MongoDB
1930
/read/distinct
2031
/read/cursors
2132

22-
- :ref:`pymongo-specify-query`
23-
- :ref:`pymongo-retrieve`
24-
- :ref:`pymongo-project`
25-
- :ref:`pymongo-specify-documents-to-return`
26-
- :ref:`pymongo-count`
27-
- :ref:`pymongo-distinct`
28-
- :ref:`pymongo-cursors`
33+
Overview
34+
--------
35+
36+
On this page, you can see copyable code examples that show common
37+
methods you can use to retrieve documents with {+driver-short+}.
38+
39+
.. tip::
40+
41+
To learn more about any of the methods shown on this page, see the link
42+
provided in each section.
43+
44+
To use an example from this page, copy the code example into the
45+
:ref:`sample application <pymongo-read-sample>` or your own application.
46+
Be sure to replace all placeholders in the code examples, such as ``<connection string URI>``, with
47+
the relevant values for your MongoDB deployment.
48+
49+
.. _pymongo-read-sample:
50+
51+
.. include:: /includes/usage-examples/sample-app-intro.rst
52+
53+
.. literalinclude:: /includes/usage-examples/read-sample-app.py
54+
:language: python
55+
:copyable:
56+
:linenos:
57+
:emphasize-lines: 10-12
58+
59+
Find One
60+
--------
61+
62+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.py
63+
:start-after: start-find-one
64+
:end-before: end-find-one
65+
:language: python
66+
:copyable:
67+
68+
To learn more about the ``find_one()`` method, see :ref:`pymongo-retrieve-find-one` in
69+
the Retrieve Data guide.
70+
71+
Find Multiple
72+
-------------
73+
74+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.py
75+
:start-after: start-find
76+
:end-before: end-find
77+
:language: python
78+
:copyable:
79+
80+
To learn more about the ``find()`` method, see :ref:`pymongo-retrieve-find-multiple` in
81+
the Retrieve Data guide.
82+
83+
Count Documents in a Collection
84+
-------------------------------
85+
86+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.py
87+
:start-after: start-count-all
88+
:end-before: end-count-all
89+
:language: python
90+
:copyable:
91+
92+
To learn more about the ``count_documents()`` method, see the
93+
:ref:`pymongo-accurate-count` guide.
94+
95+
Count Documents Returned from a Query
96+
-------------------------------------
97+
98+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.py
99+
:start-after: start-count-query
100+
:end-before: end-count-query
101+
:language: python
102+
:copyable:
103+
104+
To learn more about the ``count_documents()`` method, see the
105+
:ref:`pymongo-accurate-count` guide.
106+
107+
Estimated Document Count
108+
------------------------
109+
110+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.py
111+
:start-after: start-estimated-count
112+
:end-before: end-estimated-count
113+
:language: python
114+
:copyable:
115+
116+
To learn more about the ``estimated_document_count()`` method, see the
117+
:ref:`pymongo-estimated-count` guide.
118+
119+
Retrieve Distinct Values
120+
------------------------
121+
122+
.. literalinclude:: /includes/usage-examples/retrieve-code-examples.py
123+
:start-after: start-distinct
124+
:end-before: end-distinct
125+
:language: python
126+
:copyable:
29127

128+
To learn more about the ``distinct()`` method, see the
129+
:ref:`pymongo-distinct` guide.

source/read/count.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Overview
2323
In this guide, you can learn how to retrieve an accurate and estimated count of the
2424
number of documents in a collection.
2525

26+
.. _pymongo-accurate-count:
27+
2628
Retrieve an Accurate Count
2729
--------------------------
2830

@@ -91,6 +93,8 @@ The following table describes the options you can set to customize ``count_docum
9193
* - ``hint``
9294
- | Gets or sets the index to scan for documents.
9395

96+
.. _pymongo-estimated-count:
97+
9498
Retrieve an Estimated Count
9599
---------------------------
96100

source/security.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ the relevant values for your MongoDB deployment.
4646

4747
.. include:: /includes/usage-examples/sample-app-intro.rst
4848

49-
.. literalinclude:: /includes/usage-examples/sample-app.py
49+
.. literalinclude:: /includes/usage-examples/auth-sample-app.py
5050
:language: python
5151
:copyable: true
5252
:start-after: # start sample app

0 commit comments

Comments
 (0)