$elemMatch (query)
https://www.mongodb.com/docs/manual/reference/operator/query/elemMatch/
$elemMatch (query)
TIP
See also:
Definition
$elemMatch
-
The
$elemMatch
operator 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$elemMatch
expression, and are not using the$not
or$ne
operators inside of$elemMatch
,$elemMatch
can be omitted. See Single Query Condition.
Behavior
-
You cannot specify a
$where
expression in an$elemMatch
. -
You cannot specify a
$text
query 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
This statement inserts documents into 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, and are not using the $not
or $ne
operators inside of $elemMatch
, $elemMatch
can be omitted.
The following examples return the same documents.
With $elemMatch
:
Without $elemMatch
:
However, if your $elemMatch
expression contains the $not
or $ne
operators then omitting the $elemMatch
expression changes the documents returned.
The following examples return different documents.
With $elemMatch
:
Without $elemMatch
:
With $elemMatch
, the first query returns these documents:
Without $elemMatch
, the second query returns this document:
$elemMatch (query)
TIP
See also:
Definition
$elemMatch
-
The
$elemMatch
operator 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$elemMatch
expression, and are not using the$not
or$ne
operators inside of$elemMatch
,$elemMatch
can be omitted. See Single Query Condition.
Behavior
-
You cannot specify a
$where
expression in an$elemMatch
. -
You cannot specify a
$text
query 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
This statement inserts documents into 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, and are not using the $not
or $ne
operators inside of $elemMatch
, $elemMatch
can be omitted.
The following examples return the same documents.
With $elemMatch
:
Without $elemMatch
:
However, if your $elemMatch
expression contains the $not
or $ne
operators then omitting the $elemMatch
expression changes the documents returned.
The following examples return different documents.
With $elemMatch
:
Without $elemMatch
:
With $elemMatch
, the first query returns these documents:
Without $elemMatch
, the second query returns this document:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2015-08-12 美国L1签证申请的常见问题解析
2015-08-12 美国L1签证面谈的时候一般VO会问到什么问题?
2015-08-12 美国L-1A签证简介
2015-08-12 想去美国?看完会成功率飙升的美国签证面试技巧
2015-08-12 程序员面试揭秘之程序员靠什么途径去美国工作?
2015-08-12 国签证面试时的注意事项
2015-08-12 nearly,about,almost的区别