AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$sortByCount (집계 단계)

$sortByCount

지정된 표현식의 값을 기준으로 들어오는 문서를 그룹화한 다음 각 고유 그룹의 문서 수를 계산합니다.

각 출력 문서에는 두 개의 필드가 있습니다. 하나는 고유한 그룹화 값을 포함하는 _id 필드이고 다른 하나는 해당 그룹화 또는 범주에 속하는 문서 수를 포함하는 count 필드입니다.

문서는 내림차순으로 count 을(를) 통해 정렬됩니다.

The $sortByCount stage has the following prototype form:

{ $sortByCount: <expression> }
필드
설명

expression

그룹화할 표현식 입니다. 문서 리터럴을 제외한 모든 표현식을 지정할 수 있습니다.

필드 경로를 지정하려면 필드 이름 앞에 달러 기호 $ 를 붙이고 따옴표로 묶습니다. 예를 들어 필드를 기준으로 employee 그룹화하려면 "$employee" 표현식을 지정합니다.

{ $sortByCount: "$employee" }

Although you cannot specify a document literal for the group by expression, you can, however, specify a field or an expression that evaluates to a document. For example, if employee and business fields are document fields, then the following $mergeObjects expression, which evaluates to a document, is a valid argument to $sortByCount:

{ $sortByCount: { $mergeObjects: [ "$employee", "$business" ] } }

그러나 문서 리터럴 표현식을 사용한 다음 예는 유효하지 않습니다.

{ $sortByCount: { lname: "$employee.last", fname: "$employee.first" } }

MongoDB 6.0부터 실행에 100 메가바이트 이상의 메모리가 필요한 파이프라인 단계에서 기본적으로 임시 파일을 디스크에 씁니다. 이러한 임시 파일은 파이프라인 실행 기간 동안 지속되며 인스턴스의 스토리지 공간에 영향을 줄 수 있습니다. MongoDB의 이전 버전에서는 이 동작을 활성화하려면 개별 find 및 aggregate 명령에 { allowDiskUse: true }을 전달해야 합니다.

개별 find 및 aggregate 명령은 다음 방법 중 하나를 통해 allowDiskUseByDefault 매개변수를 재정의할 수 있습니다:

  • 1}이 로 설정된 경우 을 사용하여 임시 파일을 디스크에 쓰는 것을 허용합니다.{ allowDiskUse: true } allowDiskUseByDefault false

  • 1}이 로 설정된 경우 을 사용하여 임시 파일을 디스크에 쓰는 것을 금지합니다.{ allowDiskUse: false } allowDiskUseByDefault true

참고

For MongoDB Atlas, it is recommended to configure storage auto-scaling to prevent long-running queries from filling up storage with temporary files.

Atlas cluster 가 저장 자동 확장 을 사용하는 경우, 임시 파일로 인해 클러스터 가 다음 저장 계층 으로 확장하다 될 수 있습니다.

자세한 내용은 집계 파이프라인 제한을 참조하세요.

$sortByCount 단계는 다음 $group + $sort 시퀀스와 동일합니다.

{ $group: { _id: <expression>, count: { $sum: 1 } } },
{ $sort: { count: -1 } }

다음 문서가 포함된 exhibits 컬렉션을 생각해 보세요.

db.exhibits.insertMany(
[
{ _id: 1, title: "The Pillars of Society", artist: "Grosz", year: 1926, tags: [ "painting", "satire", "Expressionism", "caricature" ] },
{ _id: 2, title: "Melancholy III", artist: "Munch", year: 1902, tags: [ "woodcut", "Expressionism" ] },
{ _id: 3, title: "Dancer", artist: "Miro", year: 1925, tags: [ "oil", "Surrealism", "painting" ] },
{ _id: 4, title: "The Great Wave off Kanagawa", artist: "Hokusai", tags: [ "woodblock", "ukiyo-e" ] },
{ _id: 5, title: "The Persistence of Memory", artist: "Dali", year: 1931, tags: [ "Surrealism", "painting", "oil" ] },
{ _id: 6, title: "Composition VII", artist: "Kandinsky", year: 1913, tags: [ "oil", "painting", "abstract" ] },
{ _id: 7, title: "The Scream", artist: "Munch", year: 1893, tags: [ "Expressionism", "painting", "oil" ] },
{ _id: 8, title: "Blue Flower", artist: "O'Keefe", year: 1918, tags: [ "abstract", "painting" ] }
]
)

The following operation unwinds the tags array and uses the $sortByCount stage to count the number of documents associated with each tag:

db.exhibits.aggregate( [ { $unwind: "$tags" }, { $sortByCount: "$tags" } ] )

이 작업은 개수를 기준으로 내림차순으로 정렬된 다음 문서를 반환합니다.

[
{ _id: "painting", count: 6 },
{ _id: "oil", count: 4 },
{ _id: "Expressionism", count: 3 },
{ _id: "Surrealism", count: 2 },
{ _id: "abstract", count: 2 },
{ _id: "woodblock", count: 1 },
{ _id: "woodcut", count: 1 },
{ _id: "ukiyo-e", count: 1 },
{ _id: "satire", count: 1 },
{ _id: "caricature", count: 1 }
]
이 페이지 평가하기

이 페이지의 내용