来自具有可变长度的MongoDB数组中的值的变量
这是我在这里的第一个问题,非常激动地学习和道歉,如果语法不符合标记,我会随着时间的推移而进步。
“ item”
是一个数组(此文档只有一个元素)“裁决”
是一个嵌套数组,- 我想创建键 字符串长度
“ 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.
"item"
is an Array ( this doc has only one element)"adjudication"
is a nested Array with a variable number of elements(same structure)- 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 - 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
欢迎来到。我希望以下代码能够解决您的异常。有时您可能会根据自己的意愿进行修改。
$ unvind
要解构数组$ map
以通过数组循环/修改,$ arrayElementat
to to。从数组$ Let
中获取第一个对象,以通过$ split
“ pay_amt”和“ discount_amt”找到最后一个药水。因此,这将是对象的数组。但是您需要对象。因此,下一部分我将其作为k:v对。$ arrayToObject
通过使用上面的k:v对将数组变为对象。 “ v”名称。”和
( 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$split
ing 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 stepHere is the code,
Working Mongo playground
尝试此 Playground
try this playground