合并MongoDB的两个字段

发布于 2025-02-09 22:40:44 字数 858 浏览 2 评论 0原文

我使用了放松的聚合,但我有一个错误 我需要输出。其余数据也以相同的格式使用

[
  {
    "_id": "767",
    "customId": [
      "growth",
      "fine"
    ],
    "length": [
      1526,
      95
    ]
  },
  {
    "_id": "66",
    "customId": [
      "height",
      "good"
    ],
    "length": [
      86,
      68
    ]
  }
]

,我使用了放松聚合,但我有一个错误 我需要输出。其余的数据也以相同的格式

[
  {
    "_id": "767",
    "merged": [
      {
        "customId": "growth",
        "length": 1526
      },
      {
        "customId": "fine",
        "length": 95
      }
    ]
  },
  {
    "_id": "66",
    "merged": [
      {
        "customId": "height",
        "length": 86
      },
      {
        "customId": "good",
        "length": 68
      }
    ]
  }
]

我使用Postman来知道它是否有效

I used unwind aggregation but I am with an error
I need output like. the remaining data is also in the same format

[
  {
    "_id": "767",
    "customId": [
      "growth",
      "fine"
    ],
    "length": [
      1526,
      95
    ]
  },
  {
    "_id": "66",
    "customId": [
      "height",
      "good"
    ],
    "length": [
      86,
      68
    ]
  }
]

I used unwind aggregation but I am with an error
I need output like. the remaining data is also in the same format

[
  {
    "_id": "767",
    "merged": [
      {
        "customId": "growth",
        "length": 1526
      },
      {
        "customId": "fine",
        "length": 95
      }
    ]
  },
  {
    "_id": "66",
    "merged": [
      {
        "customId": "height",
        "length": 86
      },
      {
        "customId": "good",
        "length": 68
      }
    ]
  }
]

I am using postman to know whether it is working or not

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2025-02-16 22:40:44

您可以使用$ zip从两个数组中构造对,并使用$ arrayElemat来争吵您的预期输出。

db.collection.aggregate([
  {
    "$project": {
      merged: {
        "$zip": {
          "inputs": [
            "$customId",
            "$length"
          ]
        }
      }
    }
  },
  {
    "$project": {
      merged: {
        "$map": {
          "input": "$merged",
          "as": "m",
          "in": {
            "customId": {
              "$arrayElemAt": [
                "$m",
                0
              ]
            },
            "length": {
              "$arrayElemAt": [
                "$m",
                1
              ]
            }
          }
        }
      }
    }
  }
])

这是 mongo playground 供您参考。

You can use $zip to construct pairs from the 2 arrays and use $arrayElemAt to wrangle to your expected output.

db.collection.aggregate([
  {
    "$project": {
      merged: {
        "$zip": {
          "inputs": [
            "$customId",
            "$length"
          ]
        }
      }
    }
  },
  {
    "$project": {
      merged: {
        "$map": {
          "input": "$merged",
          "as": "m",
          "in": {
            "customId": {
              "$arrayElemAt": [
                "$m",
                0
              ]
            },
            "length": {
              "$arrayElemAt": [
                "$m",
                1
              ]
            }
          }
        }
      }
    }
  }
])

Here is the Mongo Playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文