定義
バージョン 5.0 の新機能。
グループにあるドキュメントの数を返します。
$count は、次のステージで使用できます。
$setWindowFields(MongoDB 5.0 以降で使用可能)
注意
曖昧さ回避
This page describes the $count aggregation accumulator. For the $count aggregation pipeline stage, see $count (aggregation pipeline).
構文
$count 構文:
{ $count: { } }
$count は、パラメータを受け入れません。
動作
$count is functionally equivalent to using { $sum : 1 } within the $group stage.
Tip
例
カリフォルニア州(CA)とワシントン州(WA)のケーキ販売を含む cakeSales コレクションを作成します。
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
次の例では、 cakeSalesコレクションが使用されています。
$group ステージで使用
This example uses $count in the $group stage to count the number of documents in the cakeSales collection for each state:
db.cakeSales.aggregate( [ { $group: { _id: "$state", countNumberOfDocumentsForState: { $count: {} } } } ] )
この例では、次のことが行われます。
_id: "$state"は、ドキュメントをstateフィールド値でグループ化します。CAとWAのグループがあります。$count: {}countNumberOfDocumentsForStateフィールドを、同じstateフィールド値を共有するドキュメントの数に設定します。
この出力では、 CAとWAのドキュメント数がcountNumberOfDocumentsForStateフィールドに表示されています。
{ "_id" : "CA", "countNumberOfDocumentsForState" : 3 } { "_id" : "WA", "countNumberOfDocumentsForState" : 3 }
$setWindowFields ステージで使用
This example uses $count in the $setWindowFields stage to count the number of documents in the cakeSales collection for each state defined in the window:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { countNumberOfDocumentsForState: { $count: {}, window: { documents: [ "unbounded", "current" ] } } } } } ] )
この例では、次のことが行われます。
partitionBy: "$state"はコレクション内のドキュメントをstateで分割します。CAとWAのパーティションがあります。sortBy: { orderDate: 1 }は、各パーティション内のドキュメントをorderDateで昇順(1)に並べ替えるので、最も古いorderDateが最初になります。
outputsets thecountNumberOfDocumentsForStatefield to the number of documents using$countthat is run in a documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument in the output. This means$countreturns the number of documents between the beginning of the partition and the current document.
この出力では、 CAとWAのドキュメント数がcountNumberOfDocumentsForStateフィールドに表示されています。
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "countNumberOfDocumentsForState" : 1 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "countNumberOfDocumentsForState" : 2 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "countNumberOfDocumentsForState" : 3 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "countNumberOfDocumentsForState" : 1 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "countNumberOfDocumentsForState" : 2 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "countNumberOfDocumentsForState" : 3 }