$elemMatch (projection)
On this page本页内容
See also: 另请参阅:
Definition定义
$elemMatch
-
The$elemMatch
operator limits the contents of an<array>
field from the query results to contain only the first element matching the$elemMatch
condition.$elemMatch
运算符将查询结果中<array>
字段的内容限制为仅包含与$elemMatch
条件匹配的第一个元素。
Usage Considerations使用注意事项
Returned Element返回的元素
Both the $
operator and the $elemMatch
operator project the first matching element from an array based on a condition.$
运算符和$elemMatch
运算符都根据条件从数组中投影第一个匹配元素。
The $
operator projects the first matching array element from each document in a collection based on some condition from the query statement.$
运算符根据查询语句中的某个条件,从集合中的每个文档中投影第一个匹配的数组元素。
The $elemMatch
projection operator takes an explicit condition argument. $elemMatch
投影运算符采用显式条件参数。This allows you to project based on a condition not in the query, or if you need to project based on multiple fields in the array's embedded documents. 这允许您基于查询中没有的条件进行投影,或者如果需要基于数组嵌入文档中的多个字段进行投影。See Array Field Limitations for an example.有关示例,请参阅数组字段限制。
Field Order字段顺序
Starting in MongoDB 4.4, regardless of the ordering of the fields in the document, the 从MongoDB 4.4开始,无论文档中字段的顺序如何,现有字段的$elemMatch
projection of an existing field returns the field after the other existing field inclusions.$elemMatch
投影都会在其他现有字段包含之后返回该字段。
For example, consider a 例如,考虑一个带有以下文档的players
collection with the following document:players
集合:
db.players.insertOne( {
name: "player1",
games: [ { game: "abc", score: 8 }, { game: "xyz", score: 5 } ],
joined: new Date("2020-01-01"),
lastLogin: new Date("2020-05-01")
} )
In version 4.4+, the following projection returns the 在4.4+版本中,以下投影会在投影中包含的其他现有字段之后返回games
field after the other existing fields included in the projection even though in the document, the field is listed before joined
and lastLogin
fields:games
字段,即使在文档中,该字段列在joind
和lastLogin
字段之前:
db.players.find( {}, { games: { $elemMatch: { score: { $gt: 5 } } }, joined: 1, lastLogin: 1 } )
That is, the operation returns the following document:也就是说,该操作返回以下文档:
{
"_id" : ObjectId("5edef64a1c099fff6b033977"),
"joined" : ISODate("2020-01-01T00:00:00Z"),
"lastLogin" : ISODate("2020-05-01T00:00:00Z"),
"games" : [ { "game" : "abc", "score" : 8 } ]
}
In version 4.2 and earlier, the 在4.2及更早版本中,现有字段的$elemMatch
projection of an existing field upholds the ordering in the document:$elemMatch
投影支持文档中的顺序:
{
"_id" : ObjectId("5edef91e76ddff7d92f118e1"),
"games" : [ { "game" : "abc", "score" : 8 } ],
"joined" : ISODate("2020-01-01T00:00:00Z"),
"lastLogin" : ISODate("2020-05-01T00:00:00Z")
}
Restrictions限制
视图上的db.collection.find()
operations on views do not support$elemMatch
projection operator.db.collection.find()
操作不支持$elemMatch
投影运算符。You cannot specify a不能在$text
query expression in an$elemMatch
.$elemMatch
中指定$text
查询表达式。
Examples实例
The examples on the $elemMatch
projection operator assumes a collection schools
with the following documents:$elemMatch
投影运算符上的示例假定集合schools
具有以下文档:
{
_id: 1,
zipcode: "63109",
students: [
{ name: "john", school: 102, age: 10 },
{ name: "jess", school: 102, age: 11 },
{ name: "jeff", school: 108, age: 15 }
]
}
{
_id: 2,
zipcode: "63110",
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 3,
zipcode: "63109",
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 4,
zipcode: "63109",
students: [
{ name: "barney", school: 102, age: 7 },
{ name: "ruth", school: 102, age: 16 },
]
}
Zipcode SearchZipcode搜索
The following 下面的find()
operation queries for all documents where the value of the zipcode
field is 63109
. find()
操作查询zipcode
字段值为63109
的所有文档。The $elemMatch
projection returns only the first matching element of the students
array where the school
field has a value of 102
:$elemMatch
投影仅返回students
数组的第一个匹配元素,其中school
字段的值为102
:
db.schools.find( { zipcode: "63109" },
{ students: { $elemMatch: { school: 102 } } } )
The operation returns the following documents that have 该操作返回以下zipcode
equal to 63109
and projects the students
array using $elemMatch
:zipcode
等于63109
的文档,并使用$elemMatch
投影students
数组:
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{ "_id" : 3 }
{ "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }
For the document with对于_id
equal to1
, thestudents
array contains multiple elements with theschool
field equal to102
. However, the$elemMatch
projection returns only the first matching element from the array._id
等于1的文档,students
数组包含school
字段等于102
的多个元素。但是,$elemMatch
投影只返回数组中的第一个匹配元素。The document with_id
equal to3
does not contain thestudents
field in the result since no element in itsstudents
array matched the$elemMatch
condition._id
等于3
的文档在结果中不包含students
字段,因为其students
数组中没有任何元素与$elemMatch
条件匹配。
$elemMatch
with Multiple Fields具有多个字段
The $elemMatch
projection can specify criteria on multiple fields:$elemMatch
投影可以在多个字段上指定条件:
The following 下面的find()
operation queries for all documents where the value of the zipcode
field is 63109
. find()
操作查询zipcode
字段值为63109
的所有文档。The projection includes the first matching element of the 投影包括students
array where the school
field has a value of 102
and the age
field is greater than 10
:students
数组的第一个匹配元素,其中school
字段的值为102
并且age
字段大于10
:
db.schools.find( { zipcode: "63109" },
{ students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
The operation returns the three documents that have 该操作返回zipcode
equal to 63109
:zipcode
等于63109
的三个文档:
{ "_id" : 1, "students" : [ { "name" : "jess", "school" : 102, "age" : 11 } ] }
{ "_id" : 3 }
{ "_id" : 4, "students" : [ { "name" : "ruth", "school" : 102, "age" : 16 } ] }
The document with _id
equal to 3
does not contain the students
field since no array element matched the $elemMatch
criteria._id
等于3
的文档不包含students
字段,因为没有数组元素与$elemMatch
条件匹配。
See also: 另请参阅:
$
(projection)(投影) operator运算符