共用方式為


$indexOfArray

$indexOfArray運算子可用來搜尋陣列中的專案,並傳回專案第一次出現的索引。 如果找不到專案,則會傳 -1回 。 此運算子適用於您需要判斷元素在數位中位置的查詢。 例如,在清單中尋找特定值或物件的索引。

語法

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

參數

參數 Description
<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 耳機」) 的位置 (索引)。

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:尋找範圍中的索引

此查詢會在特定索引範圍 (3 到 5) 內尋找陣列內 promotionEvents 「Bargain Blitz Days」促銷活動的位置,並篩選結果,並傳回前三個相符的檔

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