我可以在地图结果上使用arraytooBject mongodb吗

发布于 2025-02-10 02:03:33 字数 1270 浏览 1 评论 0原文

我有一个地图聚合管道,该管道映射值,但返回数组内部的数组。

"Codes": [
    {
        
        "$map": {
            "input": "$$e.content",
            "as": "e1",
            "in": [
                {
                    "thisCode": "$$e1.code",
                    "thisCodeDescription": "$$e1.codeDescroption"
                }
            ]
        }
        
    }
]

结果,

"Codes": [
    [
        [
            {
                "thisCode": "132",
                "thisCodeExplanation": "code stuff explanation"
            }
        ]
    ]
]

我想使用多个K,V结果与仅一项尝试并尝试过。

"Codes": [
    {
        "$arrayToObject": {
            "$map": {
                "input": "$$e.content",
                "as": "e1",
                "in": 
                    {
                        "k": "thisCode",
                        "v": "$$e1.code
                    }
                
            }
        }
    }
]

看起来这样:

"Codes": [
    {
        "thisCode": "132
    }
]

是否有一种方法可以通过在地图顶部的arraytooBject获取对象中的两个项目,从而导致两个项目而不是对象中有多个项目,而对k和v进行硬编码?

"Codes": [
    {
        "thisCode": "132",
        "thisCodeExplanation": "code stuff explanation"
    }
]

I have a map aggregate pipeline that maps the values but returns an array inside of an array.

"Codes": [
    {
        
        "$map": {
            "input": "$e.content",
            "as": "e1",
            "in": [
                {
                    "thisCode": "$e1.code",
                    "thisCodeDescription": "$e1.codeDescroption"
                }
            ]
        }
        
    }
]

results to this

"Codes": [
    [
        [
            {
                "thisCode": "132",
                "thisCodeExplanation": "code stuff explanation"
            }
        ]
    ]
]

I would like to apply an arrayToObject to it with multiple k, v results versus just one and have tried.

"Codes": [
    {
        "$arrayToObject": {
            "$map": {
                "input": "$e.content",
                "as": "e1",
                "in": 
                    {
                        "k": "thisCode",
                        "v": "$e1.code
                    }
                
            }
        }
    }
]

to look like this:

"Codes": [
    {
        "thisCode": "132
    }
]

is there a way to get both of the items in the object by arrayToObject on top of maps results to result in both items versus hard-coding the k and v if there are multiple items in the object?

"Codes": [
    {
        "thisCode": "132",
        "thisCodeExplanation": "code stuff explanation"
    }
]

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

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

发布评论

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

评论(1

还不是爱你 2025-02-17 02:03:33

不要过分复杂,您不需要$ arraytoObject,只需要删除额外的括号:

db.collection.aggregate({
  $project: {
    "Codes": {
      "$map": {
        "input": "$e.content",
        "as": "e1",
        "in": {
          "thisCode": "$e1.code",
          "thisCodeDescription": "$e1.codeDescroption"
        }
      }
    }
  }
})

Don't over complicate, you don't need $arrayToObject, you just need to remove extra brackets:

db.collection.aggregate({
  $project: {
    "Codes": {
      "$map": {
        "input": "$e.content",
        "as": "e1",
        "in": {
          "thisCode": "$e1.code",
          "thisCodeDescription": "$e1.codeDescroption"
        }
      }
    }
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文