Aracılığıyla paylaş


aggregate

aggregate komutu, veri kayıtlarını işlemek ve hesaplanan sonuçları döndürmek için kullanılır. Veriler üzerinde filtreleme, gruplandırma ve sıralama gibi işlemler gerçekleştirir ve verileri çeşitli şekillerde dönüştürebilir. Komutu aggregate çok yönlüdür ve genellikle veri analizi ve raporlama için kullanılır.

Sözdizimi

db.collection.aggregate(pipeline, options)
  • işlem hattı: Verileri işleyen ve dönüştüren toplama aşamaları dizisi.
  • seçenekler: İsteğe bağlı. Toplama için , allowDiskUseve cursorgibi explaindiğer seçenekleri belirtir.

Örnekler

Örnek 1: Kategoriye göre toplam satışları hesaplama

Bu örnekte koleksiyondaki her kategori için toplam satışların nasıl hesaplanması gösterilmektedir stores .

db.stores.aggregate([
  {
    $unwind: "$sales.salesByCategory"
  },
  {
    $group: {
      _id: "$sales.salesByCategory.categoryName",
      totalSales: { $sum: "$sales.salesByCategory.totalSales" }
    }
  }
])

Örnek çıkış verisi

[mongos] StoreData> db.stores.aggregate([
...   {
...     $unwind: "$sales.salesByCategory"
...   },
...   {
...     $group: {
...       _id: "$sales.salesByCategory.categoryName",
...       totalSales: { $sum: "$sales.salesByCategory.totalSales" }
...     }
...   }
... ])

[
  { _id: 'Christmas Trees', totalSales: 3147281 },
  { _id: 'Nuts', totalSales: 3002332 },
  { _id: 'Camping Tables', totalSales: 4431667 }
]
 

Örnek 2: Tam zamanlı personeli 10'dan büyük mağazaları bulma

Bu örnekte, tam zamanlı personel sayısının 10'dan fazla olduğu depoları filtreleme işlemi gösterilmektedir.

db.stores.aggregate([
  {
    $match: {
      "staff.totalStaff.fullTime": { $gt: 10 }
    }
  }
])

Örnek çıkış verisi

[mongos] StoreData> db.stores.aggregate([
...   {
...     $match: {
...       "staff.totalStaff.fullTime": { $gt: 10 }
...     }
...   }
... ])

[
  {
    _id: '7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5',
    name: "Lenore's DJ Equipment Store",
    location: { lat: -9.9399, lon: -0.334 },
    staff: { totalStaff: { fullTime: 18, partTime: 7 } },
    sales: {
      totalSales: 35911,
      salesByCategory: [ { categoryName: 'DJ Headphones', totalSales: 35911 } ]
    },
    promotionEvents: [
      {
        discounts: [
          { categoryName: 'DJ Turntables', discountPercentage: 18 },
          { categoryName: 'DJ Mixers', discountPercentage: 15 }
        ]
      }
    ],
    tag: [ '#SeasonalSale', '#FreeShipping', '#MembershipDeals' ]
  }
]

Örnek 3: 15%'den büyük indirimlerle tüm promosyon olaylarını listeleme

Bu örnekte, indirimin 15%'den büyük olduğu tüm promosyon olayları listelenir.

db.stores.aggregate([
  {
    $unwind: "$promotionEvents"
  },
  {
    $unwind: "$promotionEvents.discounts"
  },
  {
    $match: {
      "promotionEvents.discounts.discountPercentage": { $gt: 15 }
    }
  },
  {
    $group: {
      _id: "$promotionEvents.eventName",
      discounts: { $push: "$promotionEvents.discounts" }
    }
  }
])

Örnek çıkış verisi

[mongos] StoreData> db.stores.aggregate([
...   {
...     $unwind: "$promotionEvents"
...   },
...   {
...     $unwind: "$promotionEvents.discounts"
...   },
...   {
...     $match: {
...       "promotionEvents.discounts.discountPercentage": { $gt: 20 }
...     }
...   },
...   {
...     $group: {
...       _id: "$promotionEvents.eventName",
...       discounts: { $push: "$promotionEvents.discounts" }
...     }
...   }
... ])
[
  {
    [
      { categoryName: 'Basketball Gear', discountPercentage: 23 },
      { categoryName: 'Wool Carpets', discountPercentage: 22 },
      {
        categoryName: 'Portable Bluetooth Speakers',
        discountPercentage: 24
      }
    ]
  }
]