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