Skip to content

Commit ac9d73b

Browse files
authored
Merge pull request #128 from gmiller-mdb/DOCSP-45192-read-data-page
DOCSP-45192-read-data-page
2 parents 6ab7cf9 + 8f8396d commit ac9d73b

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'mongo'
6+
end
7+
8+
uri = '<connection string>'
9+
10+
Mongo::Client.new(uri) do |client|
11+
12+
# start-find-one
13+
document = collection.find(name: '<value>').first
14+
puts document
15+
# end-find-one
16+
17+
# start-find-many
18+
results = collection.find(founded_year: '<value>')
19+
# end-find-many
20+
21+
# start-count-collection
22+
result = collection.count_documents
23+
puts "Number of documents: #{result}"
24+
# end-count-collection
25+
26+
# start-count-accurate
27+
result = collection.count_documents('key': '<value>')
28+
puts "value: #{result}"
29+
# end-count-accurate
30+
31+
# start-count-estimate
32+
result = collection.estimated_document_count
33+
puts "Estimated number of documents: #{result}"
34+
# end-count-estimate
35+
36+
# start-distinct
37+
results = collection.distinct('field')
38+
# end-distinct
39+
40+
# start-monitor-changes
41+
stream = collection.watch
42+
collection.insert_one(a: 1)
43+
doc = stream.first
44+
process(doc)
45+
# end-monitor-changes

source/includes/usage-examples/sample-app-intro.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _ruby-read-sample:
2+
13
Sample Application
24
~~~~~~~~~~~~~~~~~~
35

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'mongo'
6+
end
7+
8+
uri = '<connection string>'
9+
10+
Mongo::Client.new(uri) do |client|
11+
database = client.use('<database name>')
12+
collection = database['<collection name>']
13+
14+
# Start example code here
15+
16+
# End example code here
17+
end

source/read.txt

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,140 @@ Read Data from MongoDB
2929
Distinct Field Values </read/distinct>
3030
Count Documents </read/count>
3131
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

Comments
 (0)