Skip to content

WRITING-906 - projection/elemMatch clarification #2125

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 3 additions & 48 deletions source/reference/operator/projection/elemMatch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ assumes a collection ``school`` with the following documents:
{ name: "jess", school: 102, age: 11 },
{ name: "jeff", school: 108, age: 15 }
]
}
},
{
_id: 2,
zipcode: "63110",
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
},
{
_id: 3,
zipcode: "63109",
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these commas aren't needed, and are kind of confusing because this isn't an array.

{
_id: 4,
zipcode: "63109",
Expand Down Expand Up @@ -126,51 +126,6 @@ The operation returns the three documents that have ``zipcode`` equal to ``63109
The document with ``_id`` equal to ``3`` does not contain the ``students`` field
since no array element matched the :projection:`$elemMatch` criteria.

:projection:`$elemMatch` with :method:`~cursor.sort()`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When the :method:`~db.collection.find()` method includes a
:method:`~cursor.sort()`, the :method:`~db.collection.find()` method
applies the :method:`~cursor.sort()` to order the matching documents
**before** it applies the projection. This is a general rule when sorting
and projecting, and is discussed in :ref:`sort-with-projection`.

If an array field contains multiple documents with the same field
name and the :method:`~db.collection.find()` method includes a
:method:`~cursor.sort()` on that repeating field, the returned
documents may not reflect the sort order because the
:method:`~cursor.sort()` was applied to the elements of the array
before the :projection:`$elemMatch` projection.

An array's sorting value is taken from either its "minimum" or "maximum" value,
depending on which way the sorting goes. The way that :method:`~cursor.sort()`
sorts documents containing arrays is described in :ref:`sort-asc-desc`.

The following query includes a :method:`~cursor.sort()` to order
by descending ``students.age`` field:

.. code-block:: javascript

db.schools.find(
{ zipcode: "63109" },
{ students: { $elemMatch: { school: 102 } } }
).sort( { "students.age": -1 } )

The operation applies the :method:`~cursor.sort()` to order the
documents that have the field ``zipcode`` equal to ``63109`` and
then applies the projection. The operation returns the three
documents in the following order:

.. code-block:: javascript

{ "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{ "_id" : 3 }

Even though the sort is descending, the younger student is listed first. This
is because the sort occurred before the older students in Barney's document were
projected out.

.. seealso::

:projection:`$ (projection) <$>` operator