Freigeben über


aggregate

Der aggregate Befehl wird verwendet, um Datensätze zu verarbeiten und berechnete Ergebnisse zurückzugeben. Sie führt Vorgänge für die Daten aus, z. B. Filtern, Gruppieren und Sortieren, und kann die Daten auf verschiedene Weise transformieren. Der aggregate Befehl ist sehr vielseitig und wird häufig für Datenanalysen und Berichte verwendet.

Syntax

db.collection.aggregate(pipeline, options)
  • Pipeline: Ein Array von Aggregationsphasen, die die Daten verarbeiten und transformieren.
  • options: Optional. Gibt weitere Optionen für die Aggregation an, z explain. B. , allowDiskUse, und cursor.

Examples

Beispiel 1: Berechnen des Gesamtumsatzes nach Kategorie

In diesem Beispiel wird veranschaulicht, wie der Gesamtumsatz für jede Kategorie in der stores Auflistung berechnet wird.

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

Beispielausgabe

[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 }
]
 

Beispiel 2: Suchen von Geschäften mit Vollzeitmitarbeitern größer als 10

In diesem Beispiel wird gezeigt, wie Sie Speicher filtern, in denen die Anzahl der Vollzeitmitarbeiter größer als 10 ist.

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

Beispielausgabe

[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' ]
  }
]

Beispiel 3: Auflisten aller Werbeaktionen mit Rabatten von mehr als 15%

In diesem Beispiel werden alle Werbeaktionsereignisse aufgelistet, bei denen ein Rabatt größer als 15%ist.

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

Beispielausgabe

[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
      }
    ]
  }
]