Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

$in (쿼리 자 연산자)

$in

$in 연산자 필드 값이 지정된 배열 의 값과 같은 문서를 선택합니다.

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

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

  • MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전

  • MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전

$in 연산자의 형식은 다음과 같습니다.

{ field: { $in: [ <value1>, <value2>, ... <valueN> ] } }

다양한 BSON types 값을 비교하려면 지정된 BSON 비교순서를 참조하십시오.

field 에 배열 있는 경우 $in 연산자 field 에 지정된 배열 의 값과 일치하는 요소가 하나 이상 포함된 배열 가진 문서를 선택합니다. 예시 들어 <value1>, <value2> 등이 있습니다.

$in 각 매개변수를 컬렉션 의 각 문서 와 비교하므로 성능 문제가 발생할 수 있습니다. 성능을 향상시키려면 쿼리 하려는 field 에 인덱스 만듭니다. 인덱스 사용하면 MongoDB 각 $in 요소에 대한 경계를 만들고 더 효율적으로 검색 수 있습니다.

참고

이 문서 $in 쿼리 연산자 대해 설명합니다.$in 집계 연산자 에 대해서는 $in(표현식 연산자)을 참조하세요.

MongoDB Atlas 에 저장된 데이터의 경우 쿼리를 실행 때 MongoDB Search in-ref 연산자 사용할 수 $search 있습니다. 다음에 $in $search 를 실행하는 것은 in-ref 연산자 사용하여 를 실행 보다 성능이 떨어집니다.$search

이 연산자 의 MongoDB Search 버전에 대해 자세히 학습 Atlas 문서에서 in-ref 연산자 참조하세요.

이 페이지의 예제에서는 sample_mflix 샘플 데이터 세트의 데이터를 사용합니다. 이 데이터 세트를 자체 관리형 MongoDB deployment 에 로드하는 방법에 대한 자세한 내용은 샘플 데이터 세트 로드를 참조하세요. 샘플 데이터베이스를 수정한 경우 이 페이지의 예제를 실행 하려면 데이터베이스를 삭제하고 다시 만들어야 할 수 있습니다.

이 쿼리는 movies 컬렉션에서 rated 필드의 값이 "G" 또는 "TV-G"인 문서를 선택합니다.

db.movies.find(
{ rated: { $in: ["G", "TV-G"] } },
{ _id: 0, title: 1 }
)
[
{ title: 'The Great Train Robbery' },
{ title: 'A Corner in Wheat' },
{ title: 'From Hand to Mouth' },
{ title: 'One Week' },
{ title: 'The Devil to Pay!' },
{ title: 'Footlight Parade' },
{ title: 'Gold Diggers of 1935' },
{ title: 'Naughty Marietta' },
{ title: 'Modern Times' },
{ title: 'Gone with the Wind' },
{ title: 'Fantasia' },
{ title: 'The Man Who Came to Dinner' },
{ title: 'National Velvet' },
{ title: 'Alice in Wonderland' },
{ title: 'The Member of the Wedding' },
{ title: 'Seven Brides for Seven Brothers' },
{ title: 'Around the World in Eighty Days' },
{ title: 'The King and I' },
{ title: 'A King in New York' },
{ title: 'Ben-Hur' }
]

연산자 사용하여 쿼리 쓰기 (write) 수 있지만 $or $in 동일한 필드 에 대해 동등성 검사를 수행할 때는 $or 연산자 대신 연산자 사용합니다.

다음 updateMany() 작업은 rated 배열에 "G" 또는 "TV-G"과 일치하는 요소가 하나 이상 있는 경우 familyFriendly 필드를 true로 설정합니다.

db.movies.updateMany(
{ rated: { $in: ["G", "TV-G"] } },
{ $set: { familyFriendly: true } }
)
{
acknowledged: true,
insertedId: null,
matchedCount: 536,
modifiedCount: 536,
upsertedCount: 0
}

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

쿼리에 대한 추가 예는 쿼리 문서를 참조하세요.

$in 연산자 /pattern/ 형식의 정규 표현식을 사용하여 문서를 선택할 수 있습니다.

이 쿼리 plot 필드 Alien 로 시작하거나 sci-fi를 포함하는 movies 컬렉션 에서 문서를 선택합니다.

db.movies.find(
{ plot: { $in: [ /^Alien/ , /sci-fi/ ] } },
{ _id: 0, title: 1, plot: 1 }
)
[
{
plot: 'Aliens come to Earth seeking scientists to help them in their war.',
title: 'This Island Earth'
},
{
plot: 'Censored by the Polish authorities, this film was reedited and new footage added. It begins with a sci-fi motif: abstract images and electronic music take the viewer from ruins of Lebanon ...',
title: 'Rece do gèry'
},
{
plot: 'An idyllic sci-fi future has one major drawback: life must end at 30.',
title: "Logan's Run"
},
{
plot: "Four horror/sci-fi segments directed by four famous directors which are their own versions of classic stories from Rod Serling's landmark television series.",
title: 'Twilight Zone: The Movie'
},
{
plot: 'Aliens who look like clowns come from outer space and terrorize a small town.',
title: 'Killer Klowns from Outer Space'
},
...
]

돌아가기

$gte

이 페이지의 내용