如何从 mongodb 中的字段创建键值对?

how to create a key value pair from fields in mongodb?

我有一些数据结构如下:

        "location_identifiers": [
            {
                "permalink": "olivette-missouri",
                "uuid": "e1774b1c-634d-4ea4-1414-cd8be15df631",
                "location_type": "city",
                "entity_def_id": "location",
                "value": "Olivette"
            },
            {
                "permalink": "missouri-united-states",
                "uuid": "51a065b8-05d5-1a28-3fcd-1ad143f1f725",
                "location_type": "region",
                "entity_def_id": "location",
                "value": "Missouri"
            }

我想重组它看起来像这样,我正在使用 mongodb 指南针:

"location_identifiers": [
            {
                "city": "Olivette",
                "region": "Missouri"
            }
]

我已经尝试过放松和项目查询,但我被卡住了,任何帮助将不胜感激。

使用$arrayToObject

db.collection.aggregate([
  {
    "$match": {}
  },
  {
    "$set": {
      "location_identifiers": [
        {
          "$arrayToObject": {
            "$map": {
              "input": "$location_identifiers",
              "as": "item",
              "in": {
                k: "$$item.location_type",
                v: "$$item.value"
              }
            }
          }
        }
      ]
    }
  }
])

mongoplayground