문서 메뉴
문서 홈
/
MongoDB 매뉴얼
/ / / /

$elemMatch (쿼리)

이 페이지의 내용

  • 정의
  • 호환성
  • 구문
  • 행동
  • 예제
  • 자세히 알아보기

다음도 참조하세요.

$elemMatch (projection)

$elemMatch

$elemMatch 연산자는 지정된 모든 쿼리 조건과 일치하는 요소가 하나 이상 있는 배열 필드가 포함된 문서를 일치시킵니다.

다음 환경에서 호스팅되는 배포에 $elemMatch 사용할 수 있습니다.

  • MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
  • $elemMatch에는 $where 표현식을 지정할 수 없습니다

  • $elemMatch에는 $text 쿼리 표현식을 지정할 수 없습니다.

scores에 다음 문서가 주어집니다

{ _id: 1, results: [ 82, 85, 88 ] }
{ _id: 2, results: [ 75, 88, 89 ] }

다음 쿼리는 results 배열에 80보다 크거나 같고 85보다 작은 요소가 하나 이상 포함된 문서만 일치합니다.

db.scores.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)

쿼리는 82 요소가 80 보다 크거나 같고 85 보다 작기 때문에 다음 문서를 반환합니다.

{ "_id" : 1, "results" : [ 82, 85, 88 ] }

배열 요소에 여러 조건을 지정하는 방법에 대한 자세한 내용은배열 요소에 여러 조건 지정을 참조하세요.

이 문은 survey 컬렉션에 문서를 삽입합니다:

db.survey.insertMany( [
{ "_id": 1, "results": [ { "product": "abc", "score": 10 },
{ "product": "xyz", "score": 5 } ] },
{ "_id": 2, "results": [ { "product": "abc", "score": 8 },
{ "product": "xyz", "score": 7 } ] },
{ "_id": 3, "results": [ { "product": "abc", "score": 7 },
{ "product": "xyz", "score": 8 } ] },
{ "_id": 4, "results": [ { "product": "abc", "score": 7 },
{ "product": "def", "score": 8 } ] },
{ "_id": 5, "results": { "product": "xyz", "score": 7 } }
] )

5_id가 있는 문서에 배열이 포함되어 있지 않습니다. 이 문서는 $elemMatch가 배열 요소와만 일치한다는 것을 보여주기 위해 포함되었으며, 다음 예시에서 볼 수 있습니다.

다음 쿼리는 resultsproduct"xyz" 이고 score8 이상인 요소가 하나 이상 포함된 문서와 일치합니다.

db.survey.find(
{ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }
)

구체적으로 쿼리는 다음 문서와 일치합니다.

{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }

다음 섹션에서는 단일 쿼리 조건과 함께 $elemMatch 을(를) 사용하고 $elemMatch 을(를) 생략할 때 출력 차이를 보여 줍니다.

$elemMatch 을(를) 사용하여 쿼리합니다.

db.survey.find(
{ results: { $elemMatch: { product: "xyz" } } }
)

쿼리는 resultsproduct"xyz"인 문서를 반환합니다.

[
{
_id: 1,
results: [ { product: 'abc', score: 10 }, { product: 'xyz', score: 5 } ]
},
{
_id: 2,
results: [ { product: 'abc', score: 8 }, { product: 'xyz', score: 7 } ]
},
{
_id: 3,
results: [ { product: 'abc', score: 7 }, { product: 'xyz', score: 8 } ]
}
]

$elemMatch 없는 쿼리:

db.survey.find(
{ "results.product": "xyz" }
)

다음 출력에서는 _id5인 문서(배열을 포함하지 않음)도 포함되어 있습니다.

[
{
_id: 1,
results: [ { product: 'abc', score: 10 }, { product: 'xyz', score: 5 } ]
},
{
_id: 2,
results: [ { product: 'abc', score: 8 }, { product: 'xyz', score: 7 } ]
},
{
_id: 3,
results: [ { product: 'abc', score: 7 }, { product: 'xyz', score: 8 } ]
},
{ _id: 5, results: { product: 'xyz', score: 7 } }
]

다음 쿼리를 고려해보세요.

  • 첫 번째 쿼리에는 $elemMatch에 단일 <query> 조건이 있습니다.

  • 두 번째 쿼리에서 $elemMatch 을(를) 생략합니다.

$elemMatch 을 사용한 첫 번째 쿼리:

db.survey.find(
{ "results": { $elemMatch: { product: { $ne: "xyz" } } } }
)

쿼리는 "xyz" 이외의 값을 가진 product 이 있는 문서를 반환합니다.

{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 },
{ "product" : "xyz", "score" : 5 } ] }
{ "_id" : 2, "results" : [ { "product" : "abc", "score" : 8 },
{ "product" : "xyz", "score" : 7 } ] }
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

$elemMatch 가 없는 두 번째 쿼리:

db.survey.find(
{ "results.product": { $ne: "xyz" } }
)

쿼리는 product results"xyz" 가 아닌 문서를 반환합니다.

{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

두 쿼리 모두 _id4인 문서를 포함하고, product"xyz"이므로 _id5인 문서를 생략합니다.

배열 쿼리에 대한 추가 예시는 다음을 참조하세요:

쿼리의 추가적인 예시는 다음을 참조하세요:

다음도 참조하세요.

돌아가기

$all

다음

$size