- Reference >
- Operators >
- Query and Projection Operators >
- Array Query Operators >
- $elemMatch (query)
$elemMatch (query)¶
On this page
See also
Definition¶
-
$elemMatch¶ The
$elemMatchoperator matches documents that contain an array field with at least one element that matches all the specified query criteria.If you specify only a single
<query>condition in the$elemMatchexpression, you do not need to use$elemMatch.
Behavior¶
- You cannot specify a
$whereexpression in an$elemMatch. - You cannot specify a
$textquery expression in an$elemMatch.
Examples¶
Element Match¶
Given the following documents in the scores collection:
The following query matches only those documents where the results
array contains at least one element that is both greater than or equal
to 80 and is less than 85.
The query returns the following document since the element 82 is
both greater than or equal to 80 and is less than 85
For more information on specifying multiple criteria on array elements, see Specify Multiple Conditions for Array Elements.
Array of Embedded Documents¶
Given the following documents in the survey collection:
The following query matches only those documents where the results
array contains at least one element with both product equal to
"xyz" and score greater than or equal to 8.
Specifically, the query matches the following document:
Single Query Condition¶
If you specify a single query predicate in the $elemMatch
expression, $elemMatch is not necessary.
For example, consider the following example where $elemMatch
specifies only a single query predicate { product: "xyz" }:
Since the $elemMatch only specifies a single condition, the
$elemMatch expression is not necessary, and instead you can
use the following query:
Additional Examples¶
For additional examples in querying arrays, see:
For additional examples in querying, see:
See also