$isArray

$isArray演算子は、指定した値が配列であるかどうかを判断するために使用されます。 値が配列の場合は true を返し、それ以外の場合は false 返します。 この演算子は、多くの場合、集計パイプラインで、フィールドに配列が含まれているかどうかに基づいてドキュメントをフィルター処理または変換するために使用されます。

構文

{
  $isArray: <expression>
}

パラメーター

パラメーター Description
<expression> チェックする値に解決される任意の有効な式。

例示

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: フィールドが配列かどうかを確認する

このクエリは、各ストア ドキュメントの sales.salesByCategory フィールドが配列であるかどうかを確認し、最初の 3 つのドキュメントの情報を返します。

db.stores.aggregate([
  {
    $project: {
      _id: 1,
      isSalesByCategoryArray: { $isArray: "$sales.salesByCategory" }
    }
  },
 // Limit the result to the first 3 documents
  { $limit: 3 } 
])

このクエリによって返される最初の 3 つの結果は次のとおりです。

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

例 2: 配列フィールドに基づくドキュメントのフィルター処理

このクエリでは、 $isArray を使用して、 promotionEvents フィールドが配列であるドキュメントをフィルター処理する方法を示します。

db.stores.aggregate([
  {
    $match: {
      $expr: { $isArray: "$promotionEvents" }
    }
  },
  // Limit the result to the first 3 documents
  { $limit: 3 },
   // Include only _id and name fields in the output 
  { $project: { _id: 1, name: 1 } }    
])

このクエリによって返される最初の 3 つの結果は次のとおりです。

[
    {
        "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41",
        "name": "VanArsdel, Ltd. | Musical Instrument Outlet - East Cassie"
    },
    {
        "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34",
        "name": "Northwind Traders | Bed and Bath Place - West Oraland"
    },
    {
        "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197",
        "name": "Wide World Importers | Bed and Bath Store - West Vitafort"
    }
]