次の方法で共有


$objectToArray

$objectToArray演算子は、ドキュメント (オブジェクト) をキーと値のペアの配列に変換するために使用されます。 結果の配列内の各キーと値のペアは、 k フィールドと v フィールドを持つドキュメントとして表されます。 この演算子は、コレクション内のドキュメントの構造を操作または分析する必要がある場合に便利です。

構文

{
  $objectToArray: <object>
}

パラメーター

パラメーター Description
<object> キーと値のペアの配列に変換されるドキュメント (オブジェクト)。

例示

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

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

例 1: location オブジェクトの変換

このクエリは、 location オブジェクトをキーと値のペアの配列に変換します。

db.stores.aggregate([
  {
    $project: {
      locationArray: { $objectToArray: "$location" }
    }
  },
  {
    $limit: 2  // Limit output to first 5 documents
  }
])

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

[
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "locationArray": [
      {
        "k": "lat",
        "v": -74.0427
      },
      {
        "k": "lon",
        "v": 160.8154
      }
    ]
  },
  {
    "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
    "locationArray": [
      {
        "k": "lat",
        "v": 61.3945
      },
      {
        "k": "lon",
        "v": -3.6196
      }
    ]
  }
]

例 2: salesByCategory 配列の変換

salesByCategory配列を変換するには、まず配列をアンワインドしてから、$objectToArray演算子を適用します。

db.stores.aggregate([
  { $unwind: "$sales.salesByCategory" },
  {
    $project: {
      salesByCategoryArray: { $objectToArray: "$sales.salesByCategory" }
    }
  },
  { 
    $limit: 2
  }
])

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

[
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "salesByCategoryArray": [
      {
        "k": "categoryName",
        "v": "Stockings"
      },
      {
        "k": "totalSales",
        "v": 25731
      }
    ]
  },
  {
    "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
    "salesByCategoryArray": [
      {
        "k": "categoryName",
        "v": "Lamps"
      },
      {
        "k": "totalSales",
        "v": 19880
      }
    ]
  }
]

サブ文書をキーと値のペアに変換することは、フィールド名を動的に処理する場合 (特に、 - ジェネリック パイプラインを構築する場合) によく使用されます。 - 柔軟な変換またはさらなる処理のために、フィールド名をキー値構造にマッピングします。