정의
$elemMatchThe
$elemMatchoperator limits the contents of an<array>field from the query results to contain only the first element matching the$elemMatchcondition.
사용 고려 사항
반환된 요소
Both the $ operator and the $elemMatch operator project the first matching element from an array based on a condition.
$ 연산자는 query문의 일부 조건을 기반으로 컬렉션의 각 문서에서 첫 번째로 일치하는 배열 요소를 프로젝트합니다.
The $elemMatch projection operator takes an explicit condition argument. 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.
필드 순서
Regardless of the ordering of the fields in the document, the $elemMatch projection of an existing field returns the field after the other existing field inclusions.
예를 들어 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") } )
다음 프로젝션은 문서에서 해당 필드가 joined 및 lastLogin 필드 앞에 나열되어 있더라도 투영에 포함된 다른 기존 필드 다음에 games 필드를 반환합니다.
db.players.find( {}, { games: { $elemMatch: { score: { $gt: 5 } } }, joined: 1, lastLogin: 1 } )
즉, 이 작업은 다음 문서를 반환합니다.
{ "_id" : ObjectId("5edef64a1c099fff6b033977"), "joined" : ISODate("2020-01-01T00:00:00Z"), "lastLogin" : ISODate("2020-05-01T00:00:00Z"), "games" : [ { "game" : "abc", "score" : 8 } ] }
제한 사항
db.collection.find()operations on views do not support$elemMatchprojection operator.You cannot specify a
$textquery operator in an$elemMatch.
예시
The examples on the $elemMatch projection operator assumes a collection schools with the following documents:
{ _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 }, ], athletics: [ "swimming", "basketball", "football" ] }, { _id: 3, zipcode: "63109", students: [ { name: "ajax", school: 100, age: 7 }, { name: "achilles", school: 100, age: 8 }, ], athletics: [ "baseball", "basketball", "soccer" ] }, { _id: 4, zipcode: "63109", students: [ { name: "barney", school: 102, age: 7 }, { name: "ruth", school: 102, age: 16 }, ] }
우편번호 검색
The operation returns the following documents that have a zipcode value of "63109" and projects the students array using $elemMatch:
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] } { "_id" : 3 } { "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }
For the document with
_idequal to1, thestudentsarray contains multiple elements with theschoolfield equal to102. However, the$elemMatchprojection returns only the first matching element from the array.The document with
_idequal to3does not contain thestudentsfield in the result since no element in itsstudentsarray matched the$elemMatchcondition.
$elemMatch 여러 필드와 함께 사용
The $elemMatch projection can specify criteria on multiple fields.
이 작업은 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.
The argument to $elemMatch matches elements of the array that $elemMatch is projecting. If you specify an equality with a field name to $elemMatch, it attempts to match objects within the array. For example, $elemMatch attempts to match objects, instead of scalar values, within the array for the following in the projection:
앞의 예에서는 zipcode 값이 "63109"인 문서를 반환하지만 프로젝션 작업에서 일치하는 요소를 찾지 못했기 때문에 이러한 문서에는 _id 필드 만 포함됩니다.
이 작업은 zipcode 값이 "63109"인 세 개의 문서를 반환합니다. 반환된 문서에는 _id 필드와 athletics 배열의 일치하는 요소가 포함되어 있습니다(있는 경우).
[ { _id: 1 }, { _id: 3, athletics: [ 'basketball' ] }, { _id: 4 } ]
The document with _id equal to 3 is the only document that matched the $elemMatch criteria.
팁
$ (projection) 연산자