Skip to content

Commit 37f9290

Browse files
authored
DOCSP-51974 $elemMatch in $match Example (#14270)
1 parent ace6c55 commit 37f9290

File tree

1 file changed

+36
-0
lines changed
  • content/manual/upcoming/source/reference/operator/aggregation

1 file changed

+36
-0
lines changed

content/manual/upcoming/source/reference/operator/aggregation/match.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,42 @@ Examples
185185

186186
{ "_id" : null, "count" : 5 }
187187

188+
Match Array Elements
189+
~~~~~~~~~~~~~~~~~~~~
190+
191+
To filter documents from a collection or previous
192+
aggregation stage based on elements in an array, use the
193+
:query:`$elemMatch` operator in the query predicate of the
194+
``$match`` stage:
195+
196+
.. io-code-block::
197+
198+
.. input::
199+
:language: javascript
200+
:emphasize-lines: 9-11
201+
202+
db.aggregate( [
203+
{
204+
$documents: [
205+
{ student_id: 1, scores: [ 0.75, 0.65, 0.73 ] },
206+
{ student_id: 2, scores: [ 0.9, 0.88, 0.98 ] },
207+
{ student_id: 3, scores: [ 0.9, 0.84, 0.93 ] }
208+
]
209+
}, {
210+
$match: {
211+
scores: { $elemMatch: { $gte: 0.9 } }
212+
}
213+
}
214+
] )
215+
216+
.. output::
217+
:language: javascript
218+
219+
[
220+
{ student_id: 2, scores: [ 0.9, 0.88, 0.98 ] },
221+
{ student_id: 3, scores: [ 0.9, 0.84, 0.93 ] }
222+
]
223+
188224
.. tab::
189225
:tabid: csharp
190226

0 commit comments

Comments
 (0)