Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
De $count operator wordt gebruikt om het aantal documenten te tellen dat overeenkomt met een opgegeven queryfilter. De operator aantal is handig voor het samenvatten van gegevens of het genereren van tellingen voor specifieke groeperingen.
Syntaxis
{
$count: "<fieldName>"
}
Parameterwaarden
| Kenmerk | Description |
|---|---|
<fieldName> |
De naam van het veld in het uitvoerdocument waarin het aantal wordt opgeslagen. |
Voorbeelden
Bekijk dit voorbeelddocument uit de winkelverzameling.
{
"_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
}
]
}
]
}
Voorbeeld 1: Het aantal documenten ophalen
Als u het aantal documenten in de verzameling wilt ophalen, voert u een tellingsquery uit zonder queryfilters.
db.stores.aggregate([{
$count: "totalDocuments"
}])
Deze query retourneert het volgende resultaat:
[
{
"totalDocuments": 41501
}
]
Voorbeeld 2: Documenten tellen gegroepeerd op een specifiek veld
Als u het aantal documenten binnen elke verkoopcategorie wilt ophalen, voert u eerst een query uit om documenten te groeperen op verkoopcategorie. Voer vervolgens een tellingsquery uit binnen elke categorie.
db.stores.aggregate([{
$unwind: "$sales.salesByCategory"
},
{
$group: {
_id: "$sales.salesByCategory.categoryName",
totalCount: {
$count: {}
}
}
}
])
Deze query retourneert de volgende resultaten:
[
{
"_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
}
]
Voorbeeld 3: Het aantal promotiegebeurtenissen tellen
Als u het aantal promotiegebeurtenissen in alle winkels wilt tellen, voert u eerst een query uit om eerst tot rust te komen door promotiegebeurtenissen en vervolgens de afzonderlijke promotiegebeurtenissen te tellen.
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$count: "totalPromotionEvents"
}
])
Deze query retourneert het volgende resultaat:
[
{
"totalPromotionEvents": 145673
}
]
Voorbeeld 4: Gebruiken $count in $setWindowFields
Als u de verkoop voor laptopspromoties per winkel wilt ophalen, voert u eerst een query uit om promotie-gebeurtenissen voor laptops in 2023 te filteren. Partitioneer vervolgens de resulterende archieven per bedrijf. Voer ten slotte een tellingsquery uit in de gepartitioneerde winkels om de resultaten te retourneren.
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"
}
}
}
])
Deze query retourneert de volgende resultaten:
[
{
"_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
}
]