運算 $count 子可用來計算符合指定查詢過濾器的文件數目。 計數運算子對於彙總資料或產生特定分組的計數非常有用。
語法
{
$count: "<fieldName>"
}
參數
| 參數 | Description |
|---|---|
<fieldName> |
輸出檔中將儲存計數的功能變數名稱。 |
範例
請參考商店集合中的此範例檔。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
範例 1:擷取所有文件的計數
若要擷取集合中的文件計數,只需執行計數查詢即可,而不使用查詢篩選器。
db.stores.aggregate([{
$count: "totalDocuments"
}])
此查詢會傳回下列結果:
[
{
"totalDocuments": 41501
}
]
範例 2:計算依特定欄位分組的文件
若要擷取每個銷售類別內的文件計數,請先執行查詢,依銷售類別將文件分組。 然後在每個類別內執行計數查詢。
db.stores.aggregate([{
$unwind: "$sales.salesByCategory"
},
{
$group: {
_id: "$sales.salesByCategory.categoryName",
totalCount: {
$count: {}
}
}
}
])
此查詢會傳回下列結果:
[
{
"_id": "Christmas Trees",
"totalCount": 93
},
{
"_id": "Nuts",
"totalCount": 83
},
{
"_id": "Camping Tables",
"totalCount": 130
},
{
"_id": "Music Theory Books",
"totalCount": 52
},
{
"_id": "Fortified Wine",
"totalCount": 55
},
{
"_id": "Children's Mystery",
"totalCount": 45
},
{
"_id": "Short Throw Projectors",
"totalCount": 72
},
{
"_id": "Pliers",
"totalCount": 56
},
{
"_id": "Bluetooth Headphones",
"totalCount": 104
},
{
"_id": "Video Storage",
"totalCount": 80
},
{
"_id": "Cleansers",
"totalCount": 68
},
{
"_id": "Camera Straps",
"totalCount": 64
},
{
"_id": "Carry-On Bags",
"totalCount": 57
},
{
"_id": "Disinfectant Wipes",
"totalCount": 85
},
{
"_id": "Insignia Smart TVs",
"totalCount": 81
},
{
"_id": "Toner Refill Kits",
"totalCount": 38
},
{
"_id": "iPads",
"totalCount": 51
},
{
"_id": "Memory Foam Mattresses",
"totalCount": 58
},
{
"_id": "Storage Baskets",
"totalCount": 68
},
{
"_id": "Body Spray",
"totalCount": 96
}
]
範例 3:計算促銷活動的數量
若要計算所有商店的促銷事件數目,請先執行查詢,先依促銷事件展開,然後計算不同的促銷事件。
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$count: "totalPromotionEvents"
}
])
此查詢會傳回下列結果:
[
{
"totalPromotionEvents": 145673
}
]
範例 4:在 中使用$count$setWindowFields
若要取得每個商店的筆記型電腦促銷銷售額,請先執行查詢以篩選 2023 年筆記型電腦的促銷活動。 然後按公司對產生的商店進行分割。 最後,跨分割存放區執行計數查詢以傳回結果。
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$unwind: "$promotionEvents.discounts"
},
// Filter only for Laptop discounts in 2023
{
$match: {
"promotionEvents.promotionalDates.startDate.Year": 2023,
"promotionEvents.discounts.categoryName": "Laptops"
}
},
// Add sales count by city using window function
{
$setWindowFields: {
partitionBy: "$company",
output: {
salesCount: {
$count: {},
window: {
documents: ["unbounded", "unbounded"]
}
}
}
}
},
// Group to return a single result per city
{
$group: {
_id: "$company",
salesCount: {
$first: "$salesCount"
}
}
}
])
此查詢會傳回下列結果:
[
{
"_id": "VanArsdel, Ltd.",
"salesCount": 13
},
{
"_id": "Proseware, Inc.",
"salesCount": 12
},
{
"_id": "Fabrikam, Inc.",
"salesCount": 11
},
{
"_id": "Contoso, Ltd.",
"salesCount": 13
},
{
"_id": "Fourth Coffee",
"salesCount": 8
},
{
"_id": "Trey Research",
"salesCount": 14
},
{
"_id": "Adatum Corporation",
"salesCount": 12
},
{
"_id": "Relecloud",
"salesCount": 16
},
{
"_id": "Lakeshore Retail",
"salesCount": 13
},
{
"_id": "Northwind Traders",
"salesCount": 5
},
{
"_id": "First Up Consultants",
"salesCount": 4
},
{
"_id": "Wide World Importers",
"salesCount": 7
},
{
"_id": "Tailwind Traders",
"salesCount": 12
}
]