Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
işleci $count , belirtilen sorgu filtresiyle eşleşen belge sayısını saymak için kullanılır. Count işleci verileri özetlemek veya belirli gruplandırmalar için sayı oluşturmak için kullanışlıdır.
Sözdizimi
{
$count: "<fieldName>"
}
Parametreler
| Parametre | Description |
|---|---|
<fieldName> |
Çıktı belgesindeki sayının depolanacağı alanın adı. |
Örnekler
Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.
{
"_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
}
]
}
]
}
Örnek 1: Tüm belgelerin sayısını alma
Koleksiyondaki belge sayısını almak için, sorgu filtreleri olmadan bir sayı sorgusu çalıştırmanız yeterlidir.
db.stores.aggregate([{
$count: "totalDocuments"
}])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"totalDocuments": 41501
}
]
Örnek 2: Belirli bir alana göre gruplandırılmış belgeleri sayma
Her satış kategorisindeki belge sayısını almak için, önce belgeleri satış kategorisine göre gruplandırmak için bir sorgu çalıştırın. Ardından her kategori içinde bir sayı sorgusu çalıştırın.
db.stores.aggregate([{
$unwind: "$sales.salesByCategory"
},
{
$group: {
_id: "$sales.salesByCategory.categoryName",
totalCount: {
$count: {}
}
}
}
])
Bu sorgu aşağıdaki sonuçları döndürür:
[
{
"_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
}
]
Örnek 3: Yükseltme olaylarının sayısını sayma
Tüm mağazalardaki promosyon olaylarının sayısını saymak için, önce bir sorgu çalıştırarak önce promosyon olaylarını geri kaldırın ve ardından ayrı yükseltme olaylarını sayın.
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$count: "totalPromotionEvents"
}
])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"totalPromotionEvents": 145673
}
]
Örnek 4: içinde kullanma $count$setWindowFields
Mağaza başına Dizüstü bilgisayar promosyonlarının satışlarını almak için önce 2023'te dizüstü bilgisayarlara yönelik promosyon olaylarını filtrelemek üzere bir sorgu çalıştırın. Ardından elde edilen depoları şirkete göre bölümleyin. Son olarak, sonuçları döndürmek için bölümlenmiş depolar arasında bir sayı sorgusu çalıştırın.
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"
}
}
}
])
Bu sorgu aşağıdaki sonuçları döndürür:
[
{
"_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
}
]