根据其他字段将字段添加到数组的每个对象

发布于 2025-01-24 21:53:43 字数 1356 浏览 3 评论 0原文

我有以下数据数组:

    {
            _id: 5f5726ef7d475a61a95c5e0c,
            attributes: [
                {
                    values: [
                        { name: '1' }
                    ],
                },
                { 
                    values: [
                        { name: '2' }
                    ]
                }
            ],
            attr1: [
                { name: "Study Code" },
                { name: "Patient Study" }
            ]
    }

我需要的是基于索引的 attr1 对象的每个对象添加通讯 value 。因此,结果是:

    {
            _id: 5f5726ef7d475a61a95c5e0c,
            attributes: [
                {
                    values: [
                        { name: '1' }
                    ],
                },
                { 
                    values: [
                        { name: '2' }
                    ]
                },
            ],
            attr1: [
                {
                    name: "Study Code",
                    values: [{ name: "1" }]
                },
                {
                    name: "Patient Study",
                    values: [{ name: "2" }]
                }
            ],
    }

我想知道是否可以在mongodb中使用contregation $ addfields

I have the following Array of data:

    {
            _id: 5f5726ef7d475a61a95c5e0c,
            attributes: [
                {
                    values: [
                        { name: '1' }
                    ],
                },
                { 
                    values: [
                        { name: '2' }
                    ]
                }
            ],
            attr1: [
                { name: "Study Code" },
                { name: "Patient Study" }
            ]
    }

What I need is to add the correspondent value to each on of attr1 objects based on index. So the result would be:

    {
            _id: 5f5726ef7d475a61a95c5e0c,
            attributes: [
                {
                    values: [
                        { name: '1' }
                    ],
                },
                { 
                    values: [
                        { name: '2' }
                    ]
                },
            ],
            attr1: [
                {
                    name: "Study Code",
                    values: [{ name: "1" }]
                },
                {
                    name: "Patient Study",
                    values: [{ name: "2" }]
                }
            ],
    }

I wonder if that possible using aggregation $addFields in MongoDB

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

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

发布评论

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

评论(2

千秋岁 2025-01-31 21:53:43

查询

  • 查询工作如果数组大小相同的
  • ziparray进行制作[[[Memort1_1 Member2_1],....]
  • 映射以合并成员1_1,成员2_1到文档

Playmongo

aggregate(
[{"$set": {"attr1": {"$zip": {"inputs": ["$attributes", "$attr1"]}}}},
 {"$set": 
   {"attr1": 
     {"$map": 
       {"input": "$attr1",
        "in": 
         {"$mergeObjects": 
           [{"$arrayElemAt": ["$this", 1]},
             {"$arrayElemAt": ["$this", 0]}]}}}}}])

Query

  • query works if arrays same size
  • ziparray to make [[member1_1 member2_1], ....]
  • map to merge member1_1,member2_1 to a document

Playmongo

aggregate(
[{"$set": {"attr1": {"$zip": {"inputs": ["$attributes", "$attr1"]}}}},
 {"$set": 
   {"attr1": 
     {"$map": 
       {"input": "$attr1",
        "in": 
         {"$mergeObjects": 
           [{"$arrayElemAt": ["$this", 1]},
             {"$arrayElemAt": ["$this", 0]}]}}}}}])
心如狂蝶 2025-01-31 21:53:43

您可以使用$ zip

db.collection.aggregate([
  {
    "$project": {
      attributes: {
        "$zip": {
          "inputs": [
            "$attributes",
            "$attr1"
          ]
        }
      }
    }
  }
])

这是 mongo Playground 供您参考。

You can use $zip

db.collection.aggregate([
  {
    "$project": {
      attributes: {
        "$zip": {
          "inputs": [
            "$attributes",
            "$attr1"
          ]
        }
      }
    }
  }
])

Here is the Mongo playground for your reference.

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