$lastN (数组表达式)

适用对象: MongoDB vCore

$lastN 运算符返回数组中的最后 n 个元素。 如果要限制从数组末尾返回的元素数,例如从按时间顺序排序的列表获取最新项,则它很有用。

语法

$lastN 运算符的语法如下:

{
  $lastN: {
    input: <array>,
    n: <number>
  }
}

参数

DESCRIPTION
input 要从中返回最后 n 个元素的数组。
n 要从数组末尾返回的元素数。 必须是正整数。

示例:

让我们了解 stores 数据集中的示例 json 的用法。

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "promotionEvents": [
    {
      "eventName": "Massive Markdown Mania",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 6, "Day": 29 },
        "endDate": { "Year": 2023, "Month": 7, "Day": 9 }
      }
    },
    {
      "eventName": "Fantastic Deal Days",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 9, "Day": 27 },
        "endDate": { "Year": 2023, "Month": 10, "Day": 7 }
      }
    },
    {
      "eventName": "Discount Delight Days",
      "promotionalDates": {
        "startDate": { "Year": 2023, "Month": 12, "Day": 26 },
        "endDate": { "Year": 2024, "Month": 1, "Day": 5 }
      }
    },
    {
      "eventName": "Super Sale Spectacular",
      "promotionalDates": {
        "startDate": { "Year": 2024, "Month": 3, "Day": 25 },
        "endDate": { "Year": 2024, "Month": 4, "Day": 2 }
      }
    },
    {
      "eventName": "Grand Deal Days",
      "promotionalDates": {
        "startDate": { "Year": 2024, "Month": 6, "Day": 23 },
        "endDate": { "Year": 2024, "Month": 6, "Day": 30 }
      }
    },
    {
      "eventName": "Major Bargain Bash",
      "promotionalDates": {
        "startDate": { "Year": 2024, "Month": 9, "Day": 21 },
        "endDate": { "Year": 2024, "Month": 9, "Day": 30 }
      }
    }
  ]
}

示例 1:获取最后两个促销事件

假设你想要从一家商店获取最近的两个促销活动。

db.stores.aggregate([
  { $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
  {
    $project: {
      name: 1,
      lastTwoPromotions: {
        $lastN: {
          input: "$promotionEvents",
          n: 2
        }
      }
    }
  }
])

这会生成以下输出:

[
  {
    _id: '40d6f4d7-50cd-4929-9a07-0a7a133c2e74',
    name: 'Proseware, Inc. | Home Entertainment Hub - East Linwoodbury',
    lastTwoPromotions: [
      {
        eventName: 'Grand Deal Days',
        promotionalDates: {
          startDate: { Year: 2024, Month: 6, Day: 23 },
          endDate: { Year: 2024, Month: 6, Day: 30 }
        },
        discounts: [
          { categoryName: 'Remote Controls', discountPercentage: 7 },
          { categoryName: 'Televisions', discountPercentage: 11 },
          {
            categoryName: 'Business Projectors',
            discountPercentage: 13
          },
          { categoryName: 'Laser Projectors', discountPercentage: 6 },
          { categoryName: 'Projectors', discountPercentage: 6 },
          { categoryName: 'Projector Screens', discountPercentage: 24 }
        ]
      },
      {
        eventName: 'Major Bargain Bash',
        promotionalDates: {
          startDate: { Year: 2024, Month: 9, Day: 21 },
          endDate: { Year: 2024, Month: 9, Day: 30 }
        },
        discounts: [
          { categoryName: 'Sound Bars', discountPercentage: 9 },
          { categoryName: 'VR Games', discountPercentage: 7 },
          { categoryName: 'Xbox Games', discountPercentage: 25 },
          {
            categoryName: 'Projector Accessories',
            discountPercentage: 18
          },
          { categoryName: 'Mobile Games', discountPercentage: 8 },
          { categoryName: 'Projector Cases', discountPercentage: 22 }
        ]
      }
    ]
  }
]

示例 2:获取最后三个销售类别

还可以用于 $lastN 获取最后几个销售类别。

db.stores.aggregate([
  { $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
  {
    $project: {
      name: 1,
      lastThreeCategories: {
        $lastN: {
          input: "$sales.salesByCategory",
          n: 3
        }
      }
    }
  }
])

这会从 salesByCategory 数组返回最后三个销售类别。

[
  {
    _id: '40d6f4d7-50cd-4929-9a07-0a7a133c2e74',
    name: 'Proseware, Inc. | Home Entertainment Hub - East Linwoodbury',
    lastThreeCategories: [
      { categoryName: 'Game Controllers', totalSales: 43522 },
      { categoryName: 'Remote Controls', totalSales: 28946 },
      { categoryName: 'VR Games', totalSales: 32272 }
    ]
  }
]