$objectToArray (数组表达式)

适用对象: MongoDB vCore

$objectToArray 运算符用于将文档(即对象)转换为键值对数组。 在需要以更灵活的数组格式操作或分析文档结构时,此运算符适用。

语法

$objectToArray 运算符的语法如下所示:

{
  $objectToArray: <object>
}

参数

说明
<object> 要转换为键值对数组的文档(或对象)。

让我们通过以下示例 json 来了解用法。

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

下方是有关如何将 $objectToArray 运算符与提供的样本 JSON 配合使用的示例。

示例 1:将地理空间字段转换为数组

以下聚合管道将 location 文档的 store 字段转换为键值对数组。

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

此查询将返回以下文档。

[
  {
    "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41",
    "locationArray": [ { "k": "lat", "v": -85.0867 }, { "k": "lon", "v": -165.3524 } ]
  },
  {
    "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34",
    "locationArray": [ { "k": "lat", "v": -22.5751 }, { "k": "lon", "v": -12.4458 } ]
  },
  {
    "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197",
    "locationArray": [ { "k": "lat", "v": -41.287 }, { "k": "lon", "v": -76.0176 } ]
  }
]

示例 2:将子文档转换为数组

此示例演示将 totalStaff 文档的 staff 字段转换为键值对数组。

db.stores.aggregate([
    {
      $project: {
        staffArray: { $objectToArray: "$staff.totalStaff" }
      }
    },
    // Limit the result to the first 3 documents
    { $limit: 3 }
])

此查询将返回以下文档。

[
  {
    "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41",
    "staffArray": [ { "k": "fullTime", "v": 15 }, { "k": "partTime", "v": 9 } ]
  },
  {
    "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34",
    "staffArray": [ { "k": "fullTime", "v": 12 }, { "k": "partTime", "v": 14 } ]
  },
  {
    "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197",
    "staffArray": [ { "k": "fullTime", "v": 4 }, { "k": "partTime", "v": 8 } ]
  }
]

示例 3:将日期/时间字段转换为数组

此管道将各个升级事件的 promotionalDates 字段转换为键值对数组。

db.stores.aggregate([
    {
      $project: {
        promotionEvents: {
          $map: {
            input: "$promotionEvents",
            as: "event",
            in: {
              eventName: "$$event.eventName",
              promotionalDatesArray: { $objectToArray: "$$event.promotionalDates" }
            }
          }
        }
      }
    },
    // Limit the result to the first document
    { $limit: 1 }
])

此查询将返回以下文档。

[
  {
    "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41",
    "promotionEvents": [
      {
        "eventName": "Markdown Mayhem",
        "promotionalDatesArray": [
          { "k": "startDate", "v": { "Year": 2024, "Month": 3, "Day": 24 } },
          { "k": "endDate", "v": { "Year": 2024, "Month": 4, "Day": 3 } }
        ]
      },
      {
        "eventName": "Spectacular Savings Showcase",
        "promotionalDatesArray": [
          { "k": "startDate", "v": { "Year": 2024, "Month": 6, "Day": 22 } },
          { "k": "endDate", "v": { "Year": 2024, "Month": 7, "Day": 1 } }
        ]
      }
    ]
  }
]