Skip to content

Commit f786188

Browse files
committed
MW PR fixes 1
1 parent e647538 commit f786188

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

source/includes/interact-data/modify-results.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
Band.limit(5)
2323
# end-limit
2424

25+
# start-skip-limit
26+
Band.skip(2).limit(5)
27+
# Skips the first two results and returns
28+
# the following five results
29+
# end-skip-limit
30+
2531
# start-skip
2632
Band.skip(3)
2733

source/interact-data/modify-results.txt

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Overview
2121
--------
2222

2323
In this guide, you can learn how to customize the way that {+odm+}
24-
returns results to you from queries. MongoDB allows you to perform the
24+
returns results from queries. MongoDB allows you to perform the
2525
following actions to modify the way that results appear:
2626

2727
- :ref:`mongoid-data-projection`
@@ -36,17 +36,17 @@ Sample Data
3636
The examples in this guide use the ``Band`` model, which represents a
3737
band or musical group. The definition of the ``Band`` model might be
3838
different for each section to demonstrate different query
39-
functionalities. Some sections might also use the ``Manager`` model,
40-
which represents a person who manages a given band, or a ``Tour`` model, which
41-
represents live performances by a certain band or musical group.
39+
functionalities. Some sections also use the ``Manager`` model,
40+
which represents a person who manages a given band, or the ``Tour``
41+
model, which represents live performances by a given band.
4242

4343
.. _mongoid-data-projection:
4444

4545
Return Specified Fields
4646
-----------------------
4747

48-
In MongoDB, the process of specifying fields to include or exclude from
49-
results is called *projection*. {+odm+} provides the following operators
48+
In MongoDB, *projection* is the process of specifying fields to include
49+
or exclude from results. {+odm+} provides the following operators
5050
to project fields:
5151

5252
- ``only()``: Specifies fields to include
@@ -98,18 +98,20 @@ Then, you can access the embedded fields from the returned documents:
9898

9999
You can pass fields of referenced associations to the ``only()`` method,
100100
but the projection is ignored when loading the embedded objects. {+odm+}
101-
loads all fields of the referenced associations.
101+
loads all fields of the referenced associations. For example, when you
102+
access the embedded ``Tour`` object as shown in the preceding code,
103+
{+odm+} returns the complete object, not just the ``year`` field.
102104

103105
.. note::
104106

105107
If you are connected to a deployment running MongoDB 4.4 or later,
106108
you cannot specify an association and its fields in a projection in
107109
the same query.
108110

109-
If a document has ``has_one`` or ``has_and_belongs_to_many``
110-
associations, you must include the fields with foreign keys in the list
111-
of attributes loaded when using ``only()`` for those associations to be
112-
loaded.
111+
If a document contains ``has_one`` or ``has_and_belongs_to_many``
112+
associations, and you want {+odm+} to load those associations when
113+
you call the ``only()`` method, you must include the fields with foreign
114+
keys in the list of attributes.
113115

114116
In the following example, the ``Band`` and ``Manager`` models have a
115117
``has_and_belongs_to_many`` association:
@@ -163,22 +165,25 @@ objects:
163165
Sort Results
164166
------------
165167

166-
You can sort the order that {+odm+} returns documents by using the
168+
You can specify the order in which {+odm+} returns documents by using the
167169
``order()`` and ``order_by()`` methods.
168170

169171
These methods accept a hash that indicates which fields to order the
170172
documents by, and whether to use an ascending or descending order for
171173
each field.
172174

173-
You can specify the sort direction in the following ways:
175+
You can specify the sort direction by using integers, symbols, or
176+
strings. We recommend using the same sorting syntax throughout your
177+
application for consistency. The following list provides each syntax and
178+
shows how to sort on the ``name`` and ``year`` fields:
174179

175180
- Integers ``1`` (ascending) and ``-1`` (descending)
176181

177182
- Example: ``Band.order(name: 1, year: -1)``
178183

179184
- Symbols ``:asc`` and ``:desc``
180185

181-
- Example: ``Band.order(name: :asc)``
186+
- Example: ``Band.order(name: :asc, year: :desc)``
182187

183188
- Strings ``"asc"`` and ``"desc"``
184189

@@ -194,49 +199,51 @@ The ``order()`` method also accepts the following sort specifications:
194199

195200
- Symbols
196201

197-
- Example: ``Band.order([[:name, :asc]])``
202+
- Example: ``Band.order([[:name, :asc], [:year, :desc]])``
198203

199204
- ``asc`` and ``desc`` methods on symbols
200205

201206
- Example: ``Band.order(:name.asc, :year.desc)``
202207

203208
- SQL syntax
204209

205-
- Example: ``Band.order('name desc')``
210+
- Example: ``Band.order('name asc', 'year desc')``
206211

207-
You can also use the ``asc()`` and ``desc()`` methods instead of using
208-
``order()``:
212+
.. tip::
209213

210-
.. code-block:: ruby
214+
Instead of using ``order()`` or ``order_by()``, you can also use the
215+
``asc()`` and ``desc()`` methods to specify sort orders:
211216

212-
Band.asc('name').desc('year')
217+
.. code-block:: ruby
218+
219+
Band.asc('name').desc('year')
213220

214-
If you chain sort specifications, the first call defines the most
215-
significant criteria and the newest call defines the least significant
216-
one.
221+
When you chain sort specifications, the first call defines the first
222+
sorting order and the newest call defines the last sorting order after
223+
the previous sorts have been applied.
217224

218225
.. TODO update link in the following note for scope
219226

220227
.. note:: Sorting in Scopes
221228

222229
If you define a scope on your model that includes a sort specification,
223-
the scope sort takes precedence over the sort specified in a query, as the
224-
default scope is evaluated first.
230+
the scope sort takes precedence over the sort specified in a query,
231+
because the default scope is evaluated first.
225232

226233
.. _mongoid-data-skip-limit:
227234

228235
Paginate Results
229236
----------------
230237

231238
{+odm+} provides the ``limit()``, ``skip()``, and ``batch_size()``
232-
pagination operators that you can use on ``Criteria`` objects. The
239+
pagination methods that you can use on ``Criteria`` objects. The
233240
following sections describe how to use these operators.
234241

235242
Limit Number of Results
236243
~~~~~~~~~~~~~~~~~~~~~~~
237244

238-
You can limit the number of results that {+odm+} returns by using the
239-
``limit()`` method.
245+
You can use the ``limit()`` method to limit the number of results that
246+
{+odm+} returns.
240247

241248
The following code retrieves a maximum of ``5`` documents:
242249

@@ -250,12 +257,21 @@ Skip Results
250257
~~~~~~~~~~~~
251258

252259
You can skip a specified number of results by using the ``skip()``
253-
method, or its alias ``offset()``. If you chain a ``limit()`` call, it
254-
is applied after documents are skipped.
260+
method, or its alias ``offset()``.
261+
262+
If you chain a ``limit()`` call to ``skip()``, the limit is applied
263+
after documents are skipped, as demonstrated in the following example:
264+
265+
.. literalinclude:: /includes/interact-data/modify-results.rb
266+
:start-after: start-skip-limit
267+
:end-before: end-skip-limit
268+
:language: ruby
269+
:dedent:
255270

256271
.. tip::
257272

258-
When performing pagination, use ``skip()`` on :ref:`sorted results <mongoid-data-sort>` to ensure consistent results.
273+
When performing pagination, use ``skip()`` on :ref:`sorted results <mongoid-data-sort>`
274+
to ensure consistent results.
259275

260276
The following code skips the first ``3`` documents when returning results:
261277

0 commit comments

Comments
 (0)