From a4c4dd88957e2a6ba8134746800e7806c6418fc5 Mon Sep 17 00:00:00 2001 From: Michael Paik Date: Thu, 22 Jan 2015 14:08:54 -0500 Subject: [PATCH] WRITING-906 - projection/elemMatch clarification --- .../operator/projection/elemMatch.txt | 51 ++----------------- 1 file changed, 3 insertions(+), 48 deletions(-) diff --git a/source/reference/operator/projection/elemMatch.txt b/source/reference/operator/projection/elemMatch.txt index 642ff65a157..4119022084d 100644 --- a/source/reference/operator/projection/elemMatch.txt +++ b/source/reference/operator/projection/elemMatch.txt @@ -38,7 +38,7 @@ assumes a collection ``school`` with the following documents: { name: "jess", school: 102, age: 11 }, { name: "jeff", school: 108, age: 15 } ] - } + }, { _id: 2, zipcode: "63110", @@ -46,7 +46,7 @@ assumes a collection ``school`` with the following documents: { name: "ajax", school: 100, age: 7 }, { name: "achilles", school: 100, age: 8 }, ] - } + }, { _id: 3, zipcode: "63109", @@ -54,7 +54,7 @@ assumes a collection ``school`` with the following documents: { name: "ajax", school: 100, age: 7 }, { name: "achilles", school: 100, age: 8 }, ] - } + }, { _id: 4, zipcode: "63109", @@ -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