-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-44849: modify results #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# start-only | ||
Band.where(members: 4).only(:name) | ||
# end-only | ||
|
||
# start-only-embed | ||
bands = Band.only(:name, 'tours.year') | ||
# end-only-embed | ||
|
||
# start-only-embed-association | ||
# Returns null | ||
Band.where(name: 'Astral Projection').only(:name).first.managers | ||
|
||
# Returns the first Manager object | ||
Band.where(name: 'Astral Projection').only(:name, :manager_ids).first.managers | ||
# end-only-embed-association | ||
|
||
# start-without | ||
Band.where(members: 4).without(:year) | ||
# end-without | ||
|
||
# start-limit | ||
Band.limit(5) | ||
# end-limit | ||
|
||
# start-skip | ||
Band.skip(3) | ||
|
||
# Equivalent | ||
Band.offset(3) | ||
# end-skip | ||
|
||
# start-batch | ||
Band.batch_size(500) | ||
# end-batch |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,281 @@ | ||||||||||||
.. _mongoid-data-modify-results: | ||||||||||||
|
||||||||||||
==================== | ||||||||||||
Modify Query Results | ||||||||||||
==================== | ||||||||||||
|
||||||||||||
.. facet:: | ||||||||||||
:name: genre | ||||||||||||
:values: reference | ||||||||||||
|
||||||||||||
.. meta:: | ||||||||||||
:keywords: ruby framework, odm, crud, print results, code example | ||||||||||||
|
||||||||||||
.. contents:: On this page | ||||||||||||
:local: | ||||||||||||
:backlinks: none | ||||||||||||
:depth: 2 | ||||||||||||
:class: singlecol | ||||||||||||
|
||||||||||||
Overview | ||||||||||||
-------- | ||||||||||||
|
||||||||||||
In this guide, you can learn how to customize the way that {+odm+} | ||||||||||||
returns results to you from queries. MongoDB allows you to perform the | ||||||||||||
following actions to modify the way that results appear: | ||||||||||||
|
||||||||||||
- :ref:`mongoid-data-projection` | ||||||||||||
|
||||||||||||
- :ref:`mongoid-data-sort` | ||||||||||||
|
||||||||||||
- :ref:`mongoid-data-skip-limit` | ||||||||||||
|
||||||||||||
Sample Data | ||||||||||||
~~~~~~~~~~~ | ||||||||||||
|
||||||||||||
The examples in this guide use the ``Band`` model, which represents a | ||||||||||||
band or musical group. The definition of the ``Band`` model might be | ||||||||||||
different for each section to demonstrate different query | ||||||||||||
functionalities. Some sections might also use the ``Manager`` model, | ||||||||||||
rustagir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
which represents a person who manages a given band, or a ``Tour`` model, which | ||||||||||||
represents live performances by a certain band or musical group. | ||||||||||||
rustagir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
.. _mongoid-data-projection: | ||||||||||||
|
||||||||||||
Return Specified Fields | ||||||||||||
----------------------- | ||||||||||||
|
||||||||||||
In MongoDB, the process of specifying fields to include or exclude from | ||||||||||||
results is called *projection*. {+odm+} provides the following operators | ||||||||||||
rustagir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
to project fields: | ||||||||||||
|
||||||||||||
- ``only()``: Specifies fields to include | ||||||||||||
- ``without()``: Specifies fields to exclude | ||||||||||||
|
||||||||||||
Include Fields | ||||||||||||
~~~~~~~~~~~~~~ | ||||||||||||
|
||||||||||||
The ``only()`` method retrieves only the specified fields from the | ||||||||||||
database. | ||||||||||||
|
||||||||||||
The following code returns only the ``name`` field from documents in | ||||||||||||
which the value of the ``members`` field is ``4``: | ||||||||||||
|
||||||||||||
.. literalinclude:: /includes/interact-data/modify-results.rb | ||||||||||||
:start-after: start-only | ||||||||||||
:end-before: end-only | ||||||||||||
:language: ruby | ||||||||||||
:dedent: | ||||||||||||
|
||||||||||||
.. note:: _id Field | ||||||||||||
|
||||||||||||
In MongoDB, the ``_id`` field is included in results even if you do | ||||||||||||
not explicitly include it. | ||||||||||||
|
||||||||||||
If you attempt to reference attributes that have not been loaded, | ||||||||||||
{+odm+} raises a ``Mongoid::Errors::AttributeNotLoaded`` error. | ||||||||||||
|
||||||||||||
You can also use the ``only()`` method to include fields from embedded | ||||||||||||
documents. | ||||||||||||
|
||||||||||||
Consider that the ``Band`` model embeds multiple ``Tour`` objects. You can | ||||||||||||
project fields from the ``Tour`` model such as ``year``, as shown in the | ||||||||||||
following code: | ||||||||||||
|
||||||||||||
.. literalinclude:: /includes/interact-data/modify-results.rb | ||||||||||||
:start-after: start-only-embed | ||||||||||||
:end-before: end-only-embed | ||||||||||||
:language: ruby | ||||||||||||
:dedent: | ||||||||||||
|
||||||||||||
Then, you can access the embedded fields from the returned documents: | ||||||||||||
|
||||||||||||
.. code-block:: ruby | ||||||||||||
|
||||||||||||
# Returns the first Tour object from | ||||||||||||
# the first Band in the results | ||||||||||||
bands.first.tours.first | ||||||||||||
|
||||||||||||
You can pass fields of referenced associations to the ``only()`` method, | ||||||||||||
but the projection is ignored when loading the embedded objects. {+odm+} | ||||||||||||
loads all fields of the referenced associations. | ||||||||||||
|
||||||||||||
|
||||||||||||
.. note:: | ||||||||||||
|
||||||||||||
If you are connected to a deployment running MongoDB 4.4 or later, | ||||||||||||
you cannot specify an association and its fields in a projection in | ||||||||||||
the same query. | ||||||||||||
|
||||||||||||
If a document has ``has_one`` or ``has_and_belongs_to_many`` | ||||||||||||
rustagir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
associations, you must include the fields with foreign keys in the list | ||||||||||||
of attributes loaded when using ``only()`` for those associations to be | ||||||||||||
loaded. | ||||||||||||
rustagir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
In the following example, the ``Band`` and ``Manager`` models have a | ||||||||||||
``has_and_belongs_to_many`` association: | ||||||||||||
|
||||||||||||
.. code-block:: ruby | ||||||||||||
|
||||||||||||
class Band | ||||||||||||
include Mongoid::Document | ||||||||||||
field :name, type: String | ||||||||||||
has_and_belongs_to_many :managers | ||||||||||||
end | ||||||||||||
|
||||||||||||
class Manager | ||||||||||||
include Mongoid::Document | ||||||||||||
has_and_belongs_to_many :bands | ||||||||||||
end | ||||||||||||
|
||||||||||||
The following code demonstrates how {+odm+} can load the associated | ||||||||||||
``Manager`` objects if you include the ``manager_ids`` field: | ||||||||||||
|
||||||||||||
.. literalinclude:: /includes/interact-data/modify-results.rb | ||||||||||||
:start-after: start-only-embed-association | ||||||||||||
:end-before: end-only-embed-association | ||||||||||||
:language: ruby | ||||||||||||
:dedent: | ||||||||||||
|
||||||||||||
Exclude Fields | ||||||||||||
~~~~~~~~~~~~~~ | ||||||||||||
|
||||||||||||
You can explicitly exclude fields from results by using the | ||||||||||||
``without()`` method. | ||||||||||||
|
||||||||||||
The following code excludes the ``year`` field from returned ``Band`` | ||||||||||||
objects: | ||||||||||||
|
||||||||||||
.. literalinclude:: /includes/interact-data/modify-results.rb | ||||||||||||
:start-after: start-without | ||||||||||||
:end-before: end-without | ||||||||||||
:language: ruby | ||||||||||||
:dedent: | ||||||||||||
|
||||||||||||
.. important:: _id Field | ||||||||||||
|
||||||||||||
{+odm+} requires the ``_id`` field for various operations, so you | ||||||||||||
*cannot* exclude the ``_id`` field or the ``id`` alias from results. | ||||||||||||
If you pass ``_id`` or ``id`` to the ``without()`` method, {+odm+} | ||||||||||||
ignores it. | ||||||||||||
|
||||||||||||
.. _mongoid-data-sort: | ||||||||||||
|
||||||||||||
Sort Results | ||||||||||||
------------ | ||||||||||||
|
||||||||||||
You can sort the order that {+odm+} returns documents by using the | ||||||||||||
|
You can sort the order that {+odm+} returns documents by using the | |
You can sort the order in which {+odm+} returns documents by using the |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: Love the code examples for each sort variation, but seems useful to give a little more context. E.g.: "The following examples show different ways to sort by the name
and year
fields."
It also took me a minute to understand these examples. Seems like you can choose the sort order by passing a number or a symbol or a string? I wonder if making that point explicitly would be helpful, and possibly organizing the info in a table or something. I assume users should also be picking just one of these methods and sticking with it, to keep their code consistent. Could be a good tip to add.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: I'd consider making order, order_by, and asc/desc their own subsections
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ill move the asc desc into a note
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: does most/least significant mean the order in which ruby will try to sort the fields by (sort the most significant, then second most, etc.)? I just haven't heard these terms before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ill reword
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I: as
the scope sort takes precedence over the sort specified in a query, as the | |
the scope sort takes precedence over the sort specified in a query, because the |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: operators vs methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are interchangeable
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: slight ambiguity between you using the limit() method and mongoid using it
You can limit the number of results that {+odm+} returns by using the | |
``limit()`` method. | |
You can use the ``limit()`` method to limit the number of results that {+odm+} returns. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: might be useful to have an example or admonition for this, actually! probably a common question.
method, or its alias ``offset()``. If you chain a ``limit()`` call, it | |
is applied after documents are skipped. | |
method, or its alias ``offset()``. If you chain a ``limit()`` call | |
to the ``skip()`` method, the limit | |
is applied after documents are skipped. |
Uh oh!
There was an error while loading. Please reload this page.