AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$sortByCount(集計ステージ)

$sortByCount

指定された式の値に基づいて受信ドキュメントをグループ化し、個別のグループごとにドキュメントの数を計算します。

各出力ドキュメントには 2 つのフィールドがあります。1 つは個別のグループ値を含む _id フィールドで、もう 1 つはそのグループまたはカテゴリに属するドキュメントの数を含む 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" } }

実行に 100 MB を超えるメモリを必要とするパイプライン ステージは、デフォルトで一時ファイルをディスクに書き込みます。これらの一時ファイルはパイプラインの実行中ずっと残り、インスタンスのストレージ容量に影響を与える可能性があります。

個々の find と aggregate コマンドは、次のいずれかの方法で allowDiskUseByDefault パラメーターを上書きできます。

  • allowDiskUseByDefaultが false に設定されている場合に { allowDiskUse: true } を使用して一時ファイルをディスクに書き込むことを許可する

  • allowDiskUseByDefault が true に設定されている場合に { allowDiskUse: false } を使用して一時ファイルがディスクに書き込むことを禁止する

注意

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

Atlas クラスターでストレージのオートスケーリングが使用されている場合、一時ファイルによってクラスターが 1 つ上のストレージ階層にスケーリングされる場合があります。

詳しくは、「集計パイプラインの制限」を参照してください。

The $sortByCount stage is equivalent to the following $group + $sort sequence:

{ $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 }
]
このページを評価