@@ -47,7 +47,6 @@ Restrictions
47
47
- You cannot specify a :query:`$text` query operator in an
48
48
:projection:`$elemMatch`.
49
49
50
-
51
50
Examples
52
51
--------
53
52
@@ -57,8 +56,7 @@ assumes a collection ``schools`` with the following documents:
57
56
.. code-block:: javascript
58
57
:copyable: true
59
58
60
- db.school.insertMany( [
61
- {
59
+ {
62
60
_id: 1,
63
61
zipcode: "63109",
64
62
students: [
@@ -93,30 +91,72 @@ assumes a collection ``schools`` with the following documents:
93
91
{ name: "ruth", school: 102, age: 16 },
94
92
]
95
93
}
96
- ] )
97
94
95
+ .. tabs-drivers::
96
+
97
+ .. tab:: MongoDB Shell
98
+ :tabid: shell
99
+
100
+ .. tab:: C#
101
+ :tabid: csharp
102
+
103
+ You can model these documents by using the following C# classes:
98
104
105
+ .. literalinclude:: /includes/driver-examples/projection/School.cs
106
+ :language: csharp
107
+
108
+ .. literalinclude:: /includes/driver-examples/projection/Student.cs
109
+ :language: csharp
99
110
100
111
Zipcode Search
101
112
~~~~~~~~~~~~~~
102
113
103
- The following :method:`~db.collection.find()` operation
104
- queries for all documents where the value of the ``zipcode``
105
- field is ``63109``. The :projection:`$elemMatch` projection
106
- returns only the **first** matching element of the ``students``
107
- array where the ``school`` field has a value of ``102``:
114
+ .. tabs-selector:: drivers
108
115
109
- .. code-block:: javascript
110
- :copyable: true
116
+ .. tabs-drivers::
117
+
118
+ .. tab:: MongoDB Shell
119
+ :tabid: shell
111
120
112
- db.schools.find( { zipcode: "63109" },
113
- { students: { $elemMatch: { school: 102 } } } )
121
+ The following :method:`~db.collection.find()` operation
122
+ queries for all documents where the value of the ``zipcode``
123
+ field is ``"63109"``. The :projection:`$elemMatch` projection
124
+ returns only the **first** matching element of the ``students``
125
+ array where the ``school`` field has a value of ``102``:
114
126
115
- The operation returns the following documents that have ``zipcode``
116
- equal to ``63109`` and projects the ``students`` array using
127
+ .. code-block:: javascript
128
+ :copyable: true
129
+
130
+ db.schools.find( { zipcode: "63109" },
131
+ { students: { $elemMatch: { school: 102 } } } )
132
+
133
+ .. tab:: C#
134
+ :tabid: csharp
135
+
136
+ To perform an ``$elemMatch`` projection when using the .NET/C# driver, call the
137
+ ``ElemMatch()`` method on the projection builder. Pass the name of the array field
138
+ to project and the filter to apply to the array elements.
139
+
140
+ The following code example finds all documents in which the value of the ``Zipcode``
141
+ field is ``"63109"``. For each matching document, the projection returns the
142
+ following fields:
143
+
144
+ - ``Id``
145
+ - The first element of the ``Students`` array in which the value of the nested
146
+ ``School`` field has the value ``102``
147
+
148
+ .. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
149
+ :language: csharp
150
+ :start-after: // start zipSearch
151
+ :end-before: // end zipSearch
152
+ :dedent: 8
153
+
154
+ The operation returns the following documents that have a ``zipcode`` value of
155
+ ``"63109"`` and projects the ``students`` array using
117
156
:projection:`$elemMatch`:
118
157
119
158
.. code-block:: javascript
159
+ :copyable: false
120
160
121
161
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
122
162
{ "_id" : 3 }
@@ -137,24 +177,47 @@ equal to ``63109`` and projects the ``students`` array using
137
177
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138
178
139
179
The :projection:`$elemMatch` projection can specify criteria on multiple
140
- fields:
180
+ fields.
141
181
142
- The following :method:`~db.collection.find()` operation
143
- queries for all documents where the value of the ``zipcode``
144
- field is ``63109``. The projection includes the **first**
145
- matching element of the ``students`` array where the ``school``
146
- field has a value of ``102`` **and** the ``age`` field is greater
147
- than ``10``:
182
+ .. tabs-drivers::
148
183
149
- .. code-block:: javascript
150
- :copyable: true
184
+ .. tab:: MongoDB Shell
185
+ :tabid: shell
186
+
187
+ The following :method:`~db.collection.find()` operation
188
+ queries for all documents where the value of the ``zipcode``
189
+ field is ``"63109"``. The projection includes the **first**
190
+ matching element of the ``students`` array where the ``school``
191
+ field has a value of ``102`` **and** the ``age`` field is greater
192
+ than ``10``:
151
193
152
- db.schools.find( { zipcode: "63109" },
153
- { students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
194
+ .. code-block:: javascript
195
+ :copyable: true
154
196
155
- The operation returns the three documents that have ``zipcode`` equal to ``63109``:
197
+ db.schools.find( { zipcode: "63109" },
198
+ { students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
199
+
200
+ .. tab:: C#
201
+ :tabid: csharp
202
+
203
+ The following code example finds all documents in which the value of the ``Zipcode``
204
+ field is ``"63109"``. For each matching document, the projection returns the following fields:
205
+
206
+ - ``Id``
207
+ - The first element of the ``Students`` array in which the value of the nested
208
+ ``School`` field has the value ``102`` and the ``Age`` field has a value greater than
209
+ ``10``
210
+
211
+ .. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
212
+ :language: csharp
213
+ :start-after: // start zipMultipleSearch
214
+ :end-before: // end zipMultipleSearch
215
+ :dedent: 8
216
+
217
+ The operation returns the three documents that have a ``zipcode`` value of ``"63109"``:
156
218
157
219
.. code-block:: javascript
220
+ :copyable: false
158
221
159
222
{ "_id" : 1, "students" : [ { "name" : "jess", "school" : 102, "age" : 11 } ] }
160
223
{ "_id" : 3 }
@@ -164,43 +227,81 @@ The document with ``_id`` equal to ``3`` does not contain the ``students`` field
164
227
since no array element matched the :projection:`$elemMatch` criteria.
165
228
166
229
The argument to :projection:`$elemMatch` matches elements of the array that
167
- ``$elemMatch`` is projecting. If you specify an equality with a field
230
+ ``$elemMatch`` is projecting. If you specify an equality with a field
168
231
name to ``$elemMatch``, it attempts to match objects within the array.
169
232
For example, ``$elemMatch`` attempts to match objects, instead of scalar
170
233
values, within the array for the following in the projection:
171
234
172
- .. code-block:: javascript
173
- :copyable: true
235
+ .. tabs-drivers::
174
236
175
- db.schools.find( { zipcode: "63109" },
176
- { athletics: { $elemMatch: { athletics: "basketball" } } })
237
+ .. tab:: MongoDB Shell
238
+ :tabid: shell
177
239
178
- To match scalar values, use the equality operator along with the scalar
179
- value that you want to match (``{$eq: <scalar value>}``). For example,
180
- the following :method:`~db.collection.find()` operation queries for all
181
- documents where the value of the ``zipcode`` field is ``63109``. The
182
- projection includes the matching element of the ``athletics`` array
183
- where the value is ``basketball``:
240
+ .. code-block:: javascript
184
241
185
- .. code-block:: javascript
186
- :copyable: true
242
+ db.schools.find( { zipcode: "63109" },
243
+ { athletics: { $elemMatch: { athletics: "basketball" } } })
244
+
245
+ .. tab:: C#
246
+ :tabid: csharp
247
+
248
+ .. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
249
+ :language: csharp
250
+ :start-after: // start zipAthleticsIncomplete
251
+ :end-before: // end zipAthleticsIncomplete
252
+ :dedent: 8
253
+
254
+ The preceding examples return the documents that have a ``zipcode`` value of
255
+ ``"63109"``, but these documents include only the ``_id`` field because the projection
256
+ operation found no matching elements.
257
+
258
+ .. tabs-drivers::
259
+
260
+ .. tab:: MongoDB Shell
261
+ :tabid: shell
262
+
263
+ To match scalar values, use the equality operator along with the scalar
264
+ value that you want to match (``{$eq: <scalar value>}``). For example,
265
+ the following :method:`~db.collection.find()` operation queries for all
266
+ documents where the value of the ``zipcode`` field is ``"63109"``. The
267
+ projection includes the matching element of the ``athletics`` array
268
+ where the value is ``basketball``:
269
+
270
+ .. code-block:: javascript
271
+ :copyable: true
272
+
273
+ db.schools.find( { zipcode: "63109" },
274
+ { athletics: { $elemMatch: { $eq: "basketball" } } })
275
+
276
+ .. tab:: C#
277
+ :tabid: csharp
278
+
279
+ To perform an ``$elemMatch`` operation against scalar values in an array when using
280
+ the .NET/C# driver, call the ``ElemMatch()`` method
281
+ on the projection builder. Pass the name of the array field to project and an
282
+ equality filter for the field ``"$eq"`` and the value you want to compare against.
187
283
188
- db.schools.find( { zipcode: "63109" },
189
- { athletics: { $elemMatch: { $eq: "basketball" } } })
284
+ .. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
285
+ :language: csharp
286
+ :start-after: // start zipAthletics
287
+ :end-before: // end zipAthletics
288
+ :dedent: 8
190
289
191
- The operation returns the three documents that have ``zipcode`` equal to
192
- ``63109``:
290
+ The operation returns the three documents that have ``zipcode`` value of
291
+ ``"63109"``. The returned documents include the ``_id`` field and matching
292
+ elements of the ``athletics`` array, if any.
193
293
194
294
.. code-block:: javascript
295
+ :copyable: false
195
296
196
297
[
197
- { _id : 1 },
298
+ { _id: 1 },
198
299
{ _id: 3, athletics: [ 'basketball' ] },
199
- { _id : 4 }
300
+ { _id: 4 }
200
301
]
201
302
202
303
The document with ``_id`` equal to ``3`` is the only document that
203
- matched the :projection:`$elemMatch` criteria.
304
+ matched the :projection:`$elemMatch` criteria.
204
305
205
306
.. seealso::
206
307
0 commit comments