$indexOfArray (数组索引函数)

$indexOfArray 运算符用于搜索数组中的元素并返回该元素第一个匹配项的索引。 如果未找到元素,则返回 -1。 此运算符适用于需要确定元素在数组中的位置的查询。 例如,在列表中查找特定值或对象的索引。

语法

{
    $indexOfArray: [ < array > , < searchElement > , < start > , < end > ]
}

parameters

参数 说明
<array> 要在其中搜索元素的数组。
<searchElement> 正在数组中搜索的元素。
<start> (可选的)要从其开始搜索的索引。 如果省略,则会从数组的开头开始搜索。
<end> (可选的)在其处结束搜索的索引。 如果省略,则搜索将一直持续到数组的末尾。

示例

请考虑商店集合中的这个示例文档。

{
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
    "location": {
        "lat": 60.1441,
        "lon": -141.5012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 2,
            "partTime": 0
        }
    },
    "sales": {
        "salesByCategory": [
            {
                "categoryName": "DJ Headphones",
                "totalSales": 35921
            }
        ],
        "fullSales": 3700
    },
    "promotionEvents": [
        {
            "eventName": "Bargain Blitz Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 3,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 2,
                    "Day": 18
                }
            },
            "discounts": [
                {
                    "categoryName": "DJ Turntables",
                    "discountPercentage": 18
                },
                {
                    "categoryName": "DJ Mixers",
                    "discountPercentage": 15
                }
            ]
        }
    ],
    "tag": [
        "#ShopLocal",
        "#SeasonalSale",
        "#FreeShipping",
        "#MembershipDeals"
    ]
}

示例 1:查找第一个匹配项的索引

此查询查找集合中 salesByCategory 数组内特定类别名称(“DJ Headphones”)的位置(索引)。

db.stores.aggregate([
  {
    $project: {
      index: {
        $indexOfArray: [
          "$sales.salesByCategory.categoryName",
          "DJ Headphones"
        ]
      }
    }
  },
  // Limit the result to the first 3 documents
  { $limit: 3 } 
])

此查询返回以下结果:

[
    {
        "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41",
        "index": -1
    },
    {
        "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34",
        "index": -1
    },
    {
        "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197",
        "index": -1
    }
]

示例 2:在范围内查找索引

此查询查找数组中数组内 promotionEvents “讨价还价闪电日”促销事件的位置(3 到 5),并筛选结果并返回前三个匹配文档

db.stores.aggregate([
  // Step 1: Project the index of the "Bargain Blitz Days" event name within the specified range
  {
    $project: {
      index: {
        $indexOfArray: [
          "$promotionEvents.eventName",
          "Bargain Blitz Days",
          3,
          5
        ]
      }
    }
  },
  // Step 2: Match documents where index > 0
  {
    $match: {
      index: { $gt: 0 }
    }
  },
 // Limit the result to the first 3 documents
  { $limit: 3 }                          
])

此查询返回以下结果:

 [
    {
        "_id": "ced8caf0-051a-48ce-88d3-2935815261c3",
        "index": 3
    },
    {
        "_id": "509be7ce-539a-41b5-8fde-b85fb3ef3faa",
        "index": 3
    },
    {
        "_id": "d06e8136-9a7f-4b08-92c8-dc8eac73bad3",
        "index": 3
    }
]