정의
그룹에 있는 문서 수를 반환합니다.
$count 축적자는 다음 단계에서 사용할 수 있습니다.
참고
명확화
This page describes the $count aggregation accumulator. You can also use $count in these other contexts:
$count (aggregation stage), 입력 문서 수를 반환합니다.$count (window function)는$setWindowFields단계에서 특정 창의 문서 수를 반환하는 데 사용됩니다.
구문
{ $count: { } }
$count 축적자는 어떤 매개변수도 허용하지 않습니다.
팁
$count 축적자는 $group 단계 내에서 { $sum: 1 }을(를) 사용하는 것과 기능적으로 동일합니다.
예시
이 페이지의 예시는 sample_mflix 데이터 세트의 데이터를 사용합니다. 이 데이터 세트를 자체 관리형 MongoDB 배포서버에 로드하는 방법에 대한 자세한 내용은 샘플 데이터세트 로드를 참조하세요. 샘플 데이터베이스를 수정한 경우, 이 페이지의 예시를 실행하려면 데이터베이스를 제거하고 다시 생성해야 할 수 있습니다.
This example uses $count in the $group stage to count the number of movies in the sample_mflix.movies collection for each selected genre:
db.movies.aggregate( [ { $unwind: "$genres" }, { $match: { genres: { $in: [ "Drama", "Short" ] } } }, { $group: { _id: "$genres", countNumberOfMoviesForGenre: { $count: {} } } }, { $sort: { _id: 1 } } ] )
예시:
$unwind: "$genres"genres배열의 각 값에 대해 문서를 만듭니다.$match문서를Drama및Short장르로만 제한합니다._id: "$genres"장르별로 문서를 그룹화합니다.$count: {}countNumberOfMoviesForGenre필드를 동일한 장르를 공유하는 영화 수로 설정합니다.
결과에는 countNumberOfMoviesForGenre 필드에서 Drama과 Short의 영화 수가 표시됩니다.