次の方法で共有


$lastN

適用対象: MongoDB 仮想コア

$lastN アキュムレータ演算子は、指定された式のドキュメントのグループ内の最後の N 個の値を返します。 これは、並べ替えられたコレクションから、最後の 1 つの値だけでなく、複数の末尾の値を取得する必要がある場合に便利です。

構文

$lastN アキュムレータ演算子の構文は次のとおりです。

{
  $group: {
    _id: <expression>,
    <field>: { 
      $lastN: {
        input: <expression>,
        n: <number>
      }
    }
  }
}

パラメーター

説明
input 最後に出現する N 個のフィールドまたは値を指定する式。
n 返される値の数。 正の整数にする必要があります。

stores データセットからのサンプル json の使用方法を理解しましょう。

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "sales": {
    "totalSales": 151864,
    "salesByCategory": [
      {
        "categoryName": "Sound Bars",
        "totalSales": 2120
      },
      {
        "categoryName": "Home Theater Projectors",
        "totalSales": 45004
      },
      {
        "categoryName": "Game Controllers",
        "totalSales": 43522
      },
      {
        "categoryName": "Remote Controls",
        "totalSales": 28946
      },
      {
        "categoryName": "VR Games",
        "totalSales": 32272
      }
    ]
  },
  "promotionEvents": [
    {
      "eventName": "Massive Markdown Mania",
      "promotionalDates": {
        "startDate": {
          "Year": 2023,
          "Month": 6,
          "Day": 29
        }
      }
    },
    {
      "eventName": "Fantastic Deal Days",
      "promotionalDates": {
        "startDate": {
          "Year": 2023,
          "Month": 9,
          "Day": 27
        }
      }
    },
    {
      "eventName": "Major Bargain Bash",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 9,
          "Day": 21
        }
      }
    }
  ]
}

例 1: 日付で最後の 2 つのプロモーション イベントを取得する

各店舗の最新の 2 つのプロモーション イベントを取得します。

db.stores.aggregate([
  { $unwind: "$promotionEvents" },
  { $sort: { 
      "promotionEvents.promotionalDates.startDate.Year": 1,
      "promotionEvents.promotionalDates.startDate.Month": 1,
      "promotionEvents.promotionalDates.startDate.Day": 1
    }
  },
  {
    $group: {
      _id: "$_id",
      storeName: { $last: "$name" },
      lastTwoPromotions: { 
        $lastN: {
          input: "$promotionEvents.eventName",
          n: 2
        }
      },
      lastTwoPromotionDates: {
        $lastN: {
          input: "$promotionEvents.promotionalDates.startDate",
          n: 2
        }
      }
    }
  }
])

これにより、各店舗の最新の 2 つのプロモーション イベントを示す出力が生成されます。

[
  {
    _id: 'e28fff9b-a8fb-4ac9-bb37-dea60d2a7d32',
    storeName: 'Lakeshore Retail | Outdoor Furniture Collection - Erdmanside',
    lastTwoPromotions: [ 'Big Bargain Bash', 'Spectacular Savings Showcase' ],
    lastTwoPromotionDates: [
      { Year: 2024, Month: 9, Day: 21 },
      { Year: 2024, Month: 6, Day: 23 }
    ]
  },
  {
    _id: '1bec7539-dc75-4f7e-b4e8-afdf8ff2f234',
    storeName: 'Adatum Corporation | Health Food Market - East Karina',
    lastTwoPromotions: [ 'Price Slash Spectacular', 'Spectacular Savings Showcase' ],
    lastTwoPromotionDates: [
      { Year: 2024, Month: 9, Day: 21 },
      { Year: 2024, Month: 6, Day: 23 }
    ]
  },
.
.
.
]

例 2: 上位 3 つの売上カテゴリを取得する

各店舗の売上上位 3 つのカテゴリを検索します。

db.stores.aggregate([
  { $unwind: "$sales.salesByCategory" },
  { $sort: { "sales.salesByCategory.totalSales": 1 } },
  {
    $group: {
      _id: "$_id",
      storeName: { $last: "$name" },
      top3Categories: { 
        $lastN: {
          input: "$sales.salesByCategory.categoryName",
          n: 3
        }
      },
      top3SalesAmounts: {
        $lastN: {
          input: "$sales.salesByCategory.totalSales",
          n: 3
        }
      }
    }
  }
])

これにより、各店舗の売上が最も高い上位 3 つのカテゴリが返されます。

[
  {
    _id: '22e6367e-8341-415f-9795-118d2b522abf',
    storeName: 'Adatum Corporation | Outdoor Furniture Mart - Port Simone',
    top3Categories: [ 'Outdoor Benches' ],
    top3SalesAmounts: [ 4976 ]
  },
  {
    _id: 'a00a3ccd-49a2-4e43-b0d9-e56b96113ed0',
    storeName: 'Wide World Importers | Smart Home Deals - Marcuschester',
    top3Categories: [ 'Smart Thermostats', 'Smart Plugs' ],
    top3SalesAmounts: [ 38696, 633 ]
  },
.
.
.
]