次の方法で共有


$indexOfArray

$indexOfArray演算子は、配列内の要素を検索し、最初に出現した要素のインデックスを返すために使用されます。 要素が見つからない場合は、 -1を返します。 この演算子は、配列内の要素の位置を決定する必要があるクエリに役立ちます。 たとえば、リスト内の特定の値またはオブジェクトのインデックスを検索します。

構文

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

パラメーター

パラメーター Description
<array> 要素を検索する配列。
<searchElement> 配列内で検索する要素。
<start> (省略可能)検索を開始するインデックス。 省略した場合、検索は配列の先頭から開始されます。
<end> (省略可能)検索を終了するインデックス。 省略すると、検索は配列の末尾まで行きます。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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: 範囲内のインデックスを検索する

このクエリは、インデックスの特定の範囲内 (3 から 5) の promotionEvents 配列内の "バーゲン ブリッツの日" プロモーション イベントの位置を検索し、最初の 3 つの一致するドキュメントを返すとともに結果をフィルター処理します

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