来自具有可变长度的MongoDB数组中的值的变量

发布于 2025-02-10 11:17:04 字数 1380 浏览 3 评论 0原文

这是我在这里的第一个问题,非常激动地学习和道歉,如果语法不符合标记,我会随着时间的推移而进步。

  1. “ item”是一个数组(此文档只有一个元素)
  2. “裁决”是一个嵌套数组,
  3. 我想创建键 字符串长度
  4. “ usevudication.Category.Coding.Code” 毫无硬编码,因为每个文档的值会有所不同,但我尝试使用>的 ” $ map“将相同的逻辑应用于每个数组元素,但
    "item": [
      {
        "adjudication": [
          {
            "amount": {"code": "USD", "system": "4217", "value": 22.51},
            "category": {
              "coding": [
                {
                  "code": "bb.org/paid_amt",
                  "system": "bb.org/adjudication"
                }
              ]
            },
            "reason": {
              "coding": [
                {
                  "code": "C",
                  "system": "bb.org/cvrg_status"
                }
              ]
            }
          },
          {
            "amount": {"code": "USD", "system": "4217", "value": 0},
            "category": {
              "coding": [
                {
                  "code": "bb.org/discount_amt",
                  "system": "bb.org/adjudication"
                }
              ]
            }
          }
        ]
      }
    ]

所需的输出失败

adjudication: {paid_amt: 22.51, discount_amt: 0}

This is my first question ever here so super excited to learn and apologies if the syntax is not up to mark, I will improve with time.

  1. "item" is an Array ( this doc has only one element)
  2. "adjudication" is a nested Array with a variable number of elements(same structure)
  3. I want to create keys out of "adjudication.category.coding.code" without hardcoding as the values will be different with each document but will have the same string length
  4. I tried using "$map" to apply the same logic to each array element but failed
    "item": [
      {
        "adjudication": [
          {
            "amount": {"code": "USD", "system": "4217", "value": 22.51},
            "category": {
              "coding": [
                {
                  "code": "bb.org/paid_amt",
                  "system": "bb.org/adjudication"
                }
              ]
            },
            "reason": {
              "coding": [
                {
                  "code": "C",
                  "system": "bb.org/cvrg_status"
                }
              ]
            }
          },
          {
            "amount": {"code": "USD", "system": "4217", "value": 0},
            "category": {
              "coding": [
                {
                  "code": "bb.org/discount_amt",
                  "system": "bb.org/adjudication"
                }
              ]
            }
          }
        ]
      }
    ]

Output desired

adjudication: {paid_amt: 22.51, discount_amt: 0}

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

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

发布评论

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

评论(2

绿光 2025-02-17 11:17:04

欢迎来到。我希望以下代码能够解决您的异常。有时您可能会根据自己的意愿进行修改。

  • $ unvind要解构数组
  • $ map以通过数组循环/修改,$ arrayElementat to to。从数组
  • $ Let中获取第一个对象,以通过$ split“ pay_amt”和“ discount_amt”找到最后一个药水。因此,这将是对象的数组。但是您需要对象。因此,下一部分我将其作为k:v对。
  • $ arrayToObject通过使用上面的k:v对将数组变为对象。 “ v”名称。
  • “ k

”和

db.collection.aggregate([
  { "$unwind": "$item" },
  {
    $project: {
      "item.adjudication": {
        "$map": {
          "input": "$item.adjudication",
          "in": {
            amount: "$this.amount.value",
            code: {
              "$arrayElemAt": [ "$this.category.coding", 0 ]
            }
          }
        }
      }
    }
  },
  {
    $project: {
      "item.adjudication": {
        "$map": {
          "input": "$item.adjudication",
          "in": {
            v: "$this.amount",
            k: {
              "$let": {
                "vars": {
                  "code": {
                    "$split": [ "$this.code.code", "/" ]
                  }
                },
                "in": { "$arrayElemAt": [ "$code",  -1 ]  }
              }
            }
          }
        }
      }
    }
  },
  {
    $project: {
      "item.adjudication": {
        "$arrayToObject": "$item.adjudication"
      }
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "item": {  "$push": "$item" }
    }
  }
])

( a href =“ https://mongoplayground.net/p/zansjadp9az” rel =“ nofollow noreferrer”> mongo playground

Welcome to SO. I hope the following code will solve your exception. Sometime you may modify based on you wish.

  • $unwind to deconstruct the array
  • $map to loop / modify through the array, and $arrayElementAt to. get the first object from the array
  • $let to find the last potion by $spliting for "paid_amt" and "discount_amt". So this will be an array of object. But you need objects. So the next part I make it as k:v pair.
  • $arrayToObject to make array to object by using above k:v pair. ("k" and "v" names are must. Can't replace by any other names)
  • $group to reconstruct the array that we did in 1st step

Here is the code,

db.collection.aggregate([
  { "$unwind": "$item" },
  {
    $project: {
      "item.adjudication": {
        "$map": {
          "input": "$item.adjudication",
          "in": {
            amount: "$this.amount.value",
            code: {
              "$arrayElemAt": [ "$this.category.coding", 0 ]
            }
          }
        }
      }
    }
  },
  {
    $project: {
      "item.adjudication": {
        "$map": {
          "input": "$item.adjudication",
          "in": {
            v: "$this.amount",
            k: {
              "$let": {
                "vars": {
                  "code": {
                    "$split": [ "$this.code.code", "/" ]
                  }
                },
                "in": { "$arrayElemAt": [ "$code",  -1 ]  }
              }
            }
          }
        }
      }
    }
  },
  {
    $project: {
      "item.adjudication": {
        "$arrayToObject": "$item.adjudication"
      }
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "item": {  "$push": "$item" }
    }
  }
])

Working Mongo playground

烟柳画桥 2025-02-17 11:17:04

尝试此 Playground

db.collection1.aggregate(
[{
    $unwind: {
        path: '$item'
    }
}, {
    $unwind: {
        path: '$item.adjudication'
    }
}, {
    $unwind: {
        path: '$item.adjudication.category'
    }
}, {
    $unwind: {
        path: '$item.adjudication.category.coding'
    }
}, {
    $group: {
        _id: null,
        data: {
            $push: {
                k: '$item.adjudication.category.coding.code',
                v: '$item.adjudication.amount.value'
            }
        }
    }
}, {
    $project: {
        _id:0,
        adjudication: {$arrayToObject: '$data'}
    }
}]
)

try this playground

db.collection1.aggregate(
[{
    $unwind: {
        path: '$item'
    }
}, {
    $unwind: {
        path: '$item.adjudication'
    }
}, {
    $unwind: {
        path: '$item.adjudication.category'
    }
}, {
    $unwind: {
        path: '$item.adjudication.category.coding'
    }
}, {
    $group: {
        _id: null,
        data: {
            $push: {
                k: '$item.adjudication.category.coding.code',
                v: '$item.adjudication.amount.value'
            }
        }
    }
}, {
    $project: {
        _id:0,
        adjudication: {$arrayToObject: '$data'}
    }
}]
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文