@@ -29,3 +29,140 @@ Read Data from MongoDB
29
29
Distinct Field Values </read/distinct>
30
30
Count Documents </read/count>
31
31
Cursors </read/cursors>
32
+
33
+ Overview
34
+ --------
35
+
36
+ On this page, you can see copyable code examples that show common {+driver-short+} methods you
37
+ can use to read data from MongoDB.
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:`ruby-read-sample` below 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
+ .. _read-sample:
50
+
51
+ .. include:: /includes/usage-examples/sample-app-intro.rst
52
+
53
+ .. literalinclude:: /includes/usage-examples/sample-read-app.rb
54
+ :language: ruby
55
+ :copyable:
56
+ :linenos:
57
+ :emphasize-lines: 14-16
58
+
59
+ Find One
60
+ --------
61
+
62
+ The following example retrieves a document that matches the criteria specified by the
63
+ given filter:
64
+
65
+ .. literalinclude:: /includes/usage-examples/read-code-examples.rb
66
+ :start-after: start-find-one
67
+ :end-before: end-find-one
68
+ :language: ruby
69
+ :copyable:
70
+ :dedent:
71
+
72
+ To learn more about the ``first`` method, see the :ref:`Retrieve Data <ruby-retrieve>`
73
+ guide.
74
+
75
+ Find Multiple
76
+ -------------
77
+
78
+ The following example retrieves all documents that match the criteria specified by the
79
+ given filter:
80
+
81
+ .. literalinclude:: /includes/usage-examples/read-code-examples.rb
82
+ :start-after: start-find-many
83
+ :end-before: end-find-many
84
+ :language: ruby
85
+ :copyable:
86
+ :dedent:
87
+
88
+ To learn more about the ``find`` method, see the :ref:`Retrieve Data <ruby-retrieve>`
89
+ guide.
90
+
91
+ Count Documents in a Collection
92
+ -------------------------------
93
+
94
+ The following example returns the number of documents in the specified collection:
95
+
96
+ .. literalinclude:: /includes/usage-examples/read-code-examples.rb
97
+ :start-after: start-count-collection
98
+ :end-before: end-count-collection
99
+ :language: ruby
100
+ :copyable:
101
+ :dedent:
102
+
103
+ To learn more about the ``count_documents`` method, see the :ref:`Count Documents <ruby-count>`
104
+ guide.
105
+
106
+ Count Documents Returned from a Query
107
+ -------------------------------------
108
+
109
+ The following example returns the number of documents in the specified
110
+ collection that match the query criteria:
111
+
112
+ .. literalinclude:: /includes/usage-examples/read-code-examples.rb
113
+ :start-after: start-count-accurate
114
+ :end-before: end-count-accurate
115
+ :language: ruby
116
+ :copyable:
117
+ :dedent:
118
+
119
+ To learn more about the ``countDocuments()`` method, see the :ref:`Count Documents <ruby-count>`
120
+ guide.
121
+
122
+ Estimated Document Count
123
+ ------------------------
124
+
125
+ The following example returns an approximate number of documents in the specified
126
+ collection based on collection metadata:
127
+
128
+ .. literalinclude:: /includes/usage-examples/read-code-examples.rb
129
+ :start-after: start-count-estimate
130
+ :end-before: end-count-estimate
131
+ :language: ruby
132
+ :copyable:
133
+ :dedent:
134
+
135
+ To learn more about the ``estimated_document_count()`` method, see the :ref:`Count Documents <ruby-count>`
136
+ guide.
137
+
138
+ Retrieve Distinct Values
139
+ ------------------------
140
+
141
+ The following example returns all distinct values of the specified field name in a given
142
+ collection:
143
+
144
+ .. literalinclude:: /includes/usage-examples/read-code-examples.rb
145
+ :start-after: start-distinct
146
+ :end-before: end-distinct
147
+ :language: ruby
148
+ :copyable:
149
+ :dedent:
150
+
151
+ To learn more about the ``distinct`` method, see the :ref:`<ruby-distinct>` guide.
152
+
153
+ Monitor Data Changes
154
+ --------------------
155
+
156
+ The following example creates a change stream for a given collection and prints out
157
+ subsequent change events in that collection:
158
+
159
+ .. literalinclude:: /includes/usage-examples/read-code-examples.rb
160
+ :start-after: start-monitor-changes
161
+ :end-before: end-monitor-changes
162
+ :language: ruby
163
+ :copyable:
164
+ :dedent:
165
+
166
+ To learn more about the ``watch()`` method, see the :ref:`Monitor Data Changes <ruby-change-streams>` guide.
167
+
168
+
0 commit comments