|
4 | 4 |
|
5 | 5 | .. default-domain:: mongodb |
6 | 6 |
|
7 | | -.. note:: |
8 | | - This page documents the use of the ``positional $`` operator in the |
9 | | - :term:`projection` document of the :method:`~db.collection.find()` |
10 | | - and the :method:`~db.collection.findOne()` methods. For the use of |
11 | | - the positional ``$`` operator with the |
12 | | - :method:`~db.collection.update()` method, see the :doc:`positional $ |
13 | | - operator for updates </reference/operator/positional>` . |
14 | | - |
15 | 7 | .. operator:: $ |
16 | 8 |
|
17 | | - *Syntax*: ``{ "<array>.$" : 1 }`` |
18 | | - |
19 | 9 | The positional :projection:`$` operator limits the contents of the |
20 | 10 | ``<array>`` field that is included in the query results to contain |
21 | | - the **first** matching element. |
| 11 | + the **first** matching element. To specify an array element to |
| 12 | + update, see the :doc:`positional $ operator for updates |
| 13 | + </reference/operator/positional>`. |
22 | 14 |
|
23 | | - Used with the :method:`~db.collection.find()` or |
24 | | - :method:`~db.collection.findOne()` method in the :term:`projection` |
25 | | - document: |
| 15 | + Used in the :term:`projection` document of the |
| 16 | + :method:`~db.collection.find()` method or the |
| 17 | + :method:`~db.collection.findOne()` method: |
26 | 18 |
|
27 | 19 | - The :projection:`$` projection operator limits the content of the |
28 | 20 | ``<array>`` field to the **first** element that matches the |
|
53 | 45 | db.collection.find( { <array>: <value>, <someOtherArray>: <value2> }, |
54 | 46 | { "<array>.$": 1 } ) |
55 | 47 |
|
56 | | - Consider a collection ``students`` with the following documents: |
| 48 | + .. example:: |
57 | 49 |
|
58 | | - .. code-block:: javascript |
| 50 | + A collection ``students`` contains the following documents: |
59 | 51 |
|
60 | | - { "_id" : 1, "semester" : 1, "grades" : [ 70, 87, 90 ] } |
61 | | - { "_id" : 2, "semester" : 1, "grades" : [ 90, 88, 92 ] } |
62 | | - { "_id" : 3, "semester" : 1, "grades" : [ 85, 100, 90 ] } |
63 | | - { "_id" : 4, "semester" : 2, "grades" : [ 79, 85, 80 ] } |
64 | | - { "_id" : 5, "semester" : 2, "grades" : [ 88, 88, 92 ] } |
65 | | - { "_id" : 6, "semester" : 2, "grades" : [ 95, 90, 96 ] } |
| 52 | + .. code-block:: javascript |
66 | 53 |
|
67 | | - The following example queries for documents where the ``semester`` |
68 | | - field equals ``1`` and the ``grades`` array contains an element |
69 | | - greater than or equal to ``85``. The example includes the |
70 | | - :projection:`$` projection operator to return only the element in |
71 | | - the ``grades`` array that matches the query criteria; the ``_id`` |
72 | | - field is by default implicitly included in the results: |
| 54 | + { "_id" : 1, "semester" : 1, "grades" : [ 70, 87, 90 ] } |
| 55 | + { "_id" : 2, "semester" : 1, "grades" : [ 90, 88, 92 ] } |
| 56 | + { "_id" : 3, "semester" : 1, "grades" : [ 85, 100, 90 ] } |
| 57 | + { "_id" : 4, "semester" : 2, "grades" : [ 79, 85, 80 ] } |
| 58 | + { "_id" : 5, "semester" : 2, "grades" : [ 88, 88, 92 ] } |
| 59 | + { "_id" : 6, "semester" : 2, "grades" : [ 95, 90, 96 ] } |
73 | 60 |
|
74 | | - .. code-block:: javascript |
| 61 | + In the following query, the projection ``{ "grades.$": 1 }`` |
| 62 | + returns only the first element greater than or equal to ``85`` |
| 63 | + for the ``grades`` field. |
75 | 64 |
|
76 | | - db.students.find( { semester: 1, grades: { $gte: 85 } }, { "grades.$": 1 } ) |
| 65 | + .. code-block:: javascript |
77 | 66 |
|
78 | | - The read operation returns the following matching documents: |
| 67 | + db.students.find( { semester: 1, grades: { $gte: 85 } }, |
| 68 | + { "grades.$": 1 } ) |
79 | 69 |
|
80 | | - .. code-block:: javascript |
| 70 | + The operation returns the following documents: |
81 | 71 |
|
82 | | - { "_id" : 1, "grades" : [ 87 ] } |
83 | | - { "_id" : 2, "grades" : [ 90 ] } |
84 | | - { "_id" : 3, "grades" : [ 85 ] } |
| 72 | + .. code-block:: javascript |
85 | 73 |
|
86 | | - Although the array field ``grades`` may contain multiple elements |
87 | | - that are greater than or equal to ``85``, the :projection:`$` |
88 | | - projection operator returns only the first matching element from the |
89 | | - array. |
| 74 | + { "_id" : 1, "grades" : [ 87 ] } |
| 75 | + { "_id" : 2, "grades" : [ 90 ] } |
| 76 | + { "_id" : 3, "grades" : [ 85 ] } |
| 77 | + |
| 78 | + Although the array field ``grades`` may contain multiple elements |
| 79 | + that are greater than or equal to ``85``, the :projection:`$` |
| 80 | + projection operator returns only the first matching element from the |
| 81 | + array. |
90 | 82 |
|
91 | 83 | .. important:: |
92 | 84 | When the :method:`~db.collection.find()` method includes a |
|
102 | 94 | applied to the elements of the array before the :projection:`$` |
103 | 95 | projection operator. |
104 | 96 |
|
105 | | - Consider the following documents in the ``students`` collection |
106 | | - where the ``grades`` field is an array of documents; each document |
107 | | - contain the three field names ``grade``, ``mean``, and ``std``: |
| 97 | + .. example:: |
108 | 98 |
|
109 | | - .. code-block:: javascript |
| 99 | + A ``students`` collection contains the following documents |
| 100 | + where the ``grades`` field is an array of documents; each document |
| 101 | + contain the three field names ``grade``, ``mean``, and ``std``: |
110 | 102 |
|
111 | | - { "_id" : 7, semester: 3, "grades" : [ { grade: 80, mean: 75, std: 8 }, |
112 | | - { grade: 85, mean: 90, std: 5 }, |
113 | | - { grade: 90, mean: 85, std: 3 } ] } |
| 103 | + .. code-block:: javascript |
114 | 104 |
|
115 | | - { "_id" : 8, semester: 3, "grades" : [ { grade: 92, mean: 88, std: 8 }, |
116 | | - { grade: 78, mean: 90, std: 5 }, |
117 | | - { grade: 88, mean: 85, std: 3 } ] } |
| 105 | + { "_id" : 7, semester: 3, "grades" : [ { grade: 80, mean: 75, std: 8 }, |
| 106 | + { grade: 85, mean: 90, std: 5 }, |
| 107 | + { grade: 90, mean: 85, std: 3 } ] } |
118 | 108 |
|
119 | | - The following example queries for documents with the array field |
120 | | - ``grades`` that contain an element with the ``mean`` greater than |
121 | | - ``70``. The query includes a projection to return the ``_id`` field |
122 | | - and only the matching array element in the ``grades`` array. The |
123 | | - query also includes a :method:`~cursor.sort()` to order by ascending |
124 | | - ``grades.grade`` field: |
| 109 | + { "_id" : 8, semester: 3, "grades" : [ { grade: 92, mean: 88, std: 8 }, |
| 110 | + { grade: 78, mean: 90, std: 5 }, |
| 111 | + { grade: 88, mean: 85, std: 3 } ] } |
125 | 112 |
|
126 | | - .. code-block:: javascript |
| 113 | + In the following query, the projection ``{ "grades.$": 1 }`` |
| 114 | + returns only the first element with the ``mean`` greater |
| 115 | + than ``70`` for the ``grades`` field. The query also includes a |
| 116 | + :method:`~cursor.sort()` to order by ascending ``grades.grade`` |
| 117 | + field: |
127 | 118 |
|
128 | | - db.students.find( { "grades.mean": { $gt: 70 } }, { "grades.$": 1 } ).sort( { "grades.grade": 1 } ) |
| 119 | + .. code-block:: javascript |
129 | 120 |
|
130 | | - The :method:`~db.collection.find()` method applies the |
131 | | - :method:`~cursor.sort()` to the matching documents **before** it |
132 | | - applies the :projection:`$` projection operator on the ``grades`` |
133 | | - array. Thus, the results with the projected array elements do not |
134 | | - reflect the ascending ``grades.grade`` sort order: |
| 121 | + db.students.find( { "grades.mean": { $gt: 70 } }, |
| 122 | + { "grades.$": 1 } |
| 123 | + ).sort( { "grades.grade": 1 } ) |
135 | 124 |
|
136 | | - .. code-block:: javascript |
| 125 | + The :method:`~db.collection.find()` method sorts the matching |
| 126 | + documents **before** it applies the :projection:`$` projection |
| 127 | + operator on the ``grades`` array. Thus, the results with the |
| 128 | + projected array elements do not reflect the ascending |
| 129 | + ``grades.grade`` sort order: |
| 130 | + |
| 131 | + .. code-block:: javascript |
137 | 132 |
|
138 | | - { "_id" : 8, "grades" : [ { "grade" : 92, "mean" : 88, "std" : 8 } ] } |
139 | | - { "_id" : 7, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 } ] } |
| 133 | + { "_id" : 8, "grades" : [ { "grade" : 92, "mean" : 88, "std" : 8 } ] } |
| 134 | + { "_id" : 7, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 } ] } |
140 | 135 |
|
141 | | - .. note:: |
| 136 | + .. note:: |
142 | 137 |
|
143 | | - Since only **one** array field can appear in the ``query |
144 | | - document``, if the array contains documents, to specify criteria |
145 | | - on multiple fields of these documents, use the |
| 138 | + Since only **one** array field can appear in the query document, |
| 139 | + if the array contains documents, to specify criteria on multiple |
| 140 | + fields of these documents, use the |
146 | 141 | :doc:`/reference/operator/elemMatch/` operator, e.g.: |
147 | 142 |
|
148 | 143 | .. code-block:: javascript |
149 | 144 |
|
150 | | - db.students.find( { grades: { $elemMatch: { mean: { $gt: 70 }, grade: { $gt:90 } } } }, |
| 145 | + db.students.find( { grades: { $elemMatch: { |
| 146 | + mean: { $gt: 70 }, |
| 147 | + grade: { $gt:90 } |
| 148 | + } } }, |
151 | 149 | { "grades.$": 1 } ) |
152 | 150 |
|
153 | 151 | .. seealso:: |
154 | | - :doc:`/reference/projection/elemMatch` |
| 152 | + :projection:`$elemMatch (projection) <$elemMatch>` |
0 commit comments