你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
$count
运算符是聚合管道中使用的累加器,用于计算文档数或分组数。 它通常用于阶段,如$group
,$bucket
或$bucketAuto
$setWindowFields
。 此运算符可用于汇总数据或生成特定分组的计数。
语法
{
$count: "<fieldName>"
}
-
<fieldName>
:指定将存储计数的输出字段的名称。
参数
参数 | DESCRIPTION |
---|---|
<fieldName> |
输出文档中将存储计数的字段的名称。 |
例子
让我们了解使用此架构构建的文档的用法。
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
}
],
"tag": [
"#ShopLocal",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
],
"company": "Lakeshore Retail",
"city": "Port Cecile",
"lastUpdated": {
"$date": "2024-12-11T10:21:58.274Z"
}
}
示例 1:统计文档总数
以下管道对集合中的文档总数进行计数。
db.stores.aggregate([
{
$count: "totalDocuments"
}
])
此查询将返回以下文档。
[
{ totalDocuments: 41501 }
]
示例 2:统计按字段分组的文档
以下管道用于 $group
计算每个文档 categoryName
的数量 salesByCategory
。
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:计数促销事件
以下管道计数的次数 promotionEvents
。
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: 'Boulder Innovations', salesCount: 10 },
{ _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 }
]