$isArray

$isArray 연산자는 지정된 값이 배열인지 확인하는 데 사용됩니다. 값이 배열 false 이고 그렇지 않으면 반환 true 됩니다. 이 연산자는 종종 집계 파이프라인에서 필드에 배열이 포함되어 있는지 여부에 따라 문서를 필터링하거나 변환하는 데 사용됩니다.

문법

{
  $isArray: <expression>
}

매개 변수

매개 변수 Description
<expression> 확인하려는 값으로 확인되는 유효한 식입니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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 배열인지 확인하고 처음 세 문서에 대한 해당 정보를 반환합니다.

db.stores.aggregate([
  {
    $project: {
      _id: 1,
      isSalesByCategoryArray: { $isArray: "$sales.salesByCategory" }
    }
  },
 // Limit the result to the first 3 documents
  { $limit: 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 } }    
])

이 쿼리에서 반환된 처음 세 가지 결과는 다음과 같습니다.

[
    {
        "_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"
    }
]