정의
호환성
다음 환경에서 호스팅되는 배포에 $filter 사용할 수 있습니다.
- MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스 
- MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전 
- MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전 
구문
$filter 의 구문은 다음과 같습니다:
{     $filter:       {          input: <array>,          as: <string>,          cond: <expression>,          limit: <number expression>       } } 
| 필드 | 사양 | 
|---|---|
| 
 | 배열로 해석되는 표현식 입니다. 
 
 | 
| 
 | 선택 사항.  | 
| 
 | 요소를 출력 배열에 포함할지 여부를 결정하는 데 사용되는 부울 값으로 해석되는 표현식입니다. 표현식은  | 
| 
 | 
표현식에 대한 자세한 내용은 표현식을 참조하세요 .
행동
| 예시 | 결과 | ||||||||
|---|---|---|---|---|---|---|---|---|---|
|  | 
 | ||||||||
|  | 
 | ||||||||
|  | 
 | 
예시
컬렉션 sales에는 다음 문서가 있습니다.
db.sales.insertMany( [    {       _id: 0,       items: [          { item_id: 43, quantity: 2, price: 10, name: "pen" },          { item_id: 2, quantity: 1, price: 240, name: "briefcase" }       ]    },    {       _id: 1,       items: [          { item_id: 23, quantity: 3, price: 110, name: "notebook" },          { item_id: 103, quantity: 4, price: 5, name: "pen" },          { item_id: 38, quantity: 1, price: 300, name: "printer" }       ]    },    {       _id: 2,       items: [          { item_id: 4, quantity: 1, price: 23, name: "paper" }       ]    } ] ) 
숫자 비교 기반 필터링
다음 예시에서는 100보다 크거나 같은 price가 있는 문서만 포함하도록 items 배열을 필터링합니다.
db.sales.aggregate( [    {       $project: {          items: {             $filter: {                input: "$items",                as: "item",                cond: { $gte: [ "$$item.price", 100 ] }             }          }       }    } ] ) 
[    {       _id: 0,       items: [ { item_id: 2, quantity: 1, price: 240, name: 'briefcase' } ]    },    {       _id: 1,       items: [          { item_id: 23, quantity: 3, price: 110, name: 'notebook' },          { item_id: 38, quantity: 1, price: 300, name: 'printer' }       ]    },    { _id: 2, items: [] } ] 
limit 필드 사용
이 예시에서는 이전 예시의 sales 컬렉션을 사용합니다.
이 예시에서는 limit 필드를 사용하여 각 items 배열에 반환된 일치하는 요소 수를 지정합니다.
db.sales.aggregate( [    {       $project: {          items: {             $filter: {                input: "$items",                as: "item",                cond: { $gte: [ "$$item.price", 100 ] },                limit: 1             }          }       }    } ] ) 
[    {       _id: 0,       items: [ { item_id: 2, quantity: 1, price: 240, name: 'briefcase' } ]    },    {       _id: 1,       items: [ { item_id: 23, quantity: 3, price: 110, name: 'notebook' } ]    },    { _id: 2, items: [] } ] 
가능한 일치값보다 큰 제한
이 예시에서는 이전 예시의 sales 컬렉션을 사용합니다.
이 예시에서는 반환될 수 있는 일치하는 요소의 수보다 큰 limit 필드 값을 사용합니다. 이 경우 limit은(는) 쿼리 결과에 영향을 주지 않으며 $gte 필터 조건과 일치하는 모든 문서를 반환합니다.
db.sales.aggregate( [    {       $project: {          items: {             $filter: {                input: "$items",                as: "item",                cond: { $gte: [ "$$item.price", 100] },                limit: 5             }          }       }    } ] ) 
[    {       _id: 0,       items: [ { item_id: 2, quantity: 1, price: 240, name: 'briefcase' } ]    },    {       _id: 1,       items: [          { item_id: 23, quantity: 3, price: 110, name: 'notebook' },          { item_id: 38, quantity: 1, price: 300, name: 'printer' }       ]    },    { _id: 2, items: [] } ] 
문자열 동등성 매치 기반 필터링
이 예시에서는 이전 예시의 sales 컬렉션을 사용합니다.
name 값이 pen인 items의 집계 필터는 다음과 같습니다.
db.sales.aggregate( [    {       $project: {          items: {             $filter: {                input: "$items",                as: "item",                cond: { $eq: [ "$$item.name", "pen"] }             }          }       }    } ] ) 
[    {       _id: 0,       items: [ { item_id: 43, quantity: 2, price: 10, name: 'pen' } ]    },    {       _id: 1,       items: [ { item_id: 103, quantity: 4, price: 5, name: 'pen' } ]    },    { _id: 2, items: [] } ] 
정규 표현식 일치 기반 필터링
이 예시에서는 이전 예시의 sales 컬렉션을 사용합니다.
다음 집계는 $regexMatch을(를) 사용하여 p(으)로 시작하는 name 값이 있는 items을(를) 필터링합니다.
db.sales.aggregate( [    {       $project: {          items: {             $filter: {                input: "$items",                as: "item",                cond: {                   $regexMatch: { input: "$$item.name", regex: /^p/ }                }             }          }       }    } ] ) 
[    {       _id: 0,       items: [ { item_id: 43, quantity: 2, price: 10, name: 'pen' } ]    },    {       _id: 1,       items: [          { item_id: 103, quantity: 4, price: 5, name: 'pen' },          { item_id: 38, quantity: 1, price: 300, name: 'printer' }       ]    },    {       _id: 2,       items: [ { item_id: 4, quantity: 1, price: 23, name: 'paper' } ]    } ]