如何从阵列中一起添加单行文档

时间:2017-06-18 19:22:19

标签: mongodb mongodb-query aggregation-framework

我有一个包含这样一个条目的集合:

{
"gstin": "22AAAAA1301E1T8",
"sname": "RR&Co",
"fp": "112016",
"gt": 1234567.89,
"curr_gt": 1232.00,
"b2b": [{
        "ctin": "32DS233TY781233",
        "inum": "TEST12345",
        "idt": "15-11-2016",
        "ival": 20000.01,
        "pos": "GJ",
        "rchrg": "N",
        "type": "Regular",
        "itms": [{
            "num": 1,
            "itm_det": {
                "rt": 5,
                "txval": 15000.23,
                "iamt": 1000.20,
                "camt": 1000.25,
                "samt": 0,
                "csamt": 0
            },
            "itc": {
                "elg": "ip",
                "tc_i": 0.0,
                "tc_c": 0.0,
                "tc_s": 0.0,
                "tc_cs": 0.0
            }
        },
        {
            "num": 2,
            "itm_det": {
                "rt": 6,
                "txval": 16000.23,
                "iamt": 1600.20,
                "camt": 1600.25,
                "samt": 100,
                "csamt": 50
            },
            "itc": {
                "elg": "ip",
                "tc_i": 2.0,
                "tc_c": 1.0,
                "tc_s": 3.0,
                "tc_cs": 5.0
            }
        }],
        "custom": {
            "custom_<<attr1_name>>": "",
            "custom_<<attr2_name>>": "",
            "custom_<<attr3_name>>": "",
            "custom_<<attr4_name>>": ""
        }
    },
    {
        "ctin": "32DS233TY781234",
        "inum": "GUEST12345",
        "idt": "18-11-2016",
        "ival": 10000.01,
        "pos": "GJ",
        "rchrg": "Y",
        "type": "RD",
        "itms": [{
            "num": 1,
            "itm_det": {
                "rt": 6,
                "txval": 14000.23,
                "iamt": 1200.20,
                "camt": 1300.25,
                "samt": 0,
                "csamt": 0
            },
            "itc": {
                "elg": "ip",
                "tc_i": 0.0,
                "tc_c": 0.0,
                "tc_s": 0.0,
                "tc_cs": 0.0
            }
        }],
        "custom": {
            "custom_<<attr1_name>>": "",
            "custom_<<attr2_name>>": "",
            "custom_<<attr3_name>>": "",
            "custom_<<attr4_name>>": ""
        }
    },
    {
        "ctin": "32DS233TY781235",
        "inum": "HOST12345",
        "idt": "18-11-2016",
        "ival": 20000.01,
        "pos": "GJ",
        "rchrg": "Y",
        "type": "RD",
        "itms": [{
            "num": 1,
            "itm_det": {
                "rt": 5,
                "txval": 16000.23,
                "iamt": 1300.20,
                "camt": 1400.25,
                "samt": 0,
                "csamt": 0
            },
            "itc": {
                "elg": "ip",
                "tc_i": 0.0,
                "tc_c": 0.0,
                "tc_s": 0.0,
                "tc_cs": 0.0
            }
        }],
        "custom": {
            "custom_<<attr1_name>>": "",
            "custom_<<attr2_name>>": "",
            "custom_<<attr3_name>>": "",
            "custom_<<attr4_name>>": ""
        }
    }
],
"imp_g": [{
    "port_code": "INATT2",
    "gstin": "27AHQPA8875L1ZU",
    "boe_num": "25662",
    "boe_dt": "18-04-2016",
    "boe_val": 23456.20,
    "txval": 100000.00,
    "type": "Import",
    "itms": [{
        "num": 1,
        "itm_det": {
            "rt": 5,
            "txval": 15000.23,
            "iamt": 1000.20,
            "camt": 1000.25,
            "samt": 0,
            "csamt": 0
        },
        "itc": {
            "elg": "ip",
            "tc_i": 0.0,
            "tc_c": 0.0,
            "tc_s": 0.0,
            "tc_cs": 0.0
        }
    }],
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"cdnr": [{
    "ctin": "32DS233TY781223",
    "ntty": "credit",
    "nt_num": "123356a",
    "nt_dt": "13-04-2016",
    "pos": "MH",
    "itms": [{
        "num": 1,
        "itm_det": {
            "rt": 5,
            "txval": 15000.23,
            "iamt": 1000.20,
            "camt": 1000.25,
            "samt": 0,
            "csamt": 0
        },
        "itc": {
            "elg": "ip",
            "tc_i": 0.0,
            "tc_c": 0.0,
            "tc_s": 0.0,
            "tc_cs": 0.0
        }
    }],
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"nil": [{
    "sply_ty": "INTRB2B",
    "cpddr": 123.45,
    "exptdsply": 123.45,
    "nildsply": 1470.85,
    "ngsply": 1258.5,
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"isd": [{
    "gstin": "123456a",
    "ty": "isd",
    "inum": "1213A",
    "idt": "13-04-2016",
    "itms": [{
        "num": 1,
        "itm_det": {
            "ig_cr": 123,
            "cg_cr": 12,
            "sg_cr": 18,
            "cs_cr": 9
        },
        "itc": {
            "ig_amt": 123.0,
            "cg_amt": 234.0,
            "sg_amt": 345.00,
            "cs_amt": 1000.25
        }
    }],
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"tctd": [{
    "ty": "tds",
    "ctin": "123456a",
    "grval": 123.00,
    "slval": 234.00,
    "nt_val": 345.00,
    "iamt": 14.52,
    "camt": 78.52,
    "samt": 12.9,
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"atr": [{
    "ty": "P",
    "ad_amt": 10000.00,
    "month": "mm",
    "inter": [{
        "rt": 18,
        "txval": 15000.23,
        "iamt": 0,
        "csamt": 0,
        "pos": "GJ"
    }, {
        "rt": 18,
        "txval": 15000.23,
        "iamt": 0,
        "csamt": 0,
        "pos": "KA"
    }],
    "intra": [{
        "rt": 18,
        "txval": 15000.23,
        "crt": 9.00,
        "camt": 1000.25,
        "srt": 9.00,
        "samt": 0,
        "csamt": 0
    }],
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"itc": [{
    "amt_type": "Amount in terms of rule 2(2) of ITC Rules",
    "action": "ad",
    "iamt": 14.52,
    "camt": 78.52,
    "samt": 12.9,
    "csamt": 12.3,
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"itcm": [{
    "mis_type": "ITC claimed on mismatched/duplication of invoice /debit notes",
    "action": "add",
    "iamt": 14.52,
    "camt": 78.52,
    "samt": 12.9,
    "csamt": 12.3,
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}],
"hsn": [{
    "num": 1,
    "ty": "hsn",
    "desc": "hsnsummary",
    "uqc": "1",
    "qty": 2.05,
    "txval": 10.23,
    "iamt": 14.52,
    "camt": 78.52,
    "samt": 12.9,
    "csamt": 500,
    "val": 1000,
    "custom": {
        "custom_<<attr1_name>>": "",
        "custom_<<attr2_name>>": "",
        "custom_<<attr3_name>>": "",
        "custom_<<attr4_name>>": ""
    }
}]
}

现在我想为每个'ctin'显示每个'b2b'什么是总'txval'和('iamt','camt','samt')的总和。每个itms可能在一个数组中有多个文档。

2 个答案:

答案 0 :(得分:0)

您可以使用$map$sum运算符通过聚合框架从MongoDB获取结果:

db.horrible.aggregate([
  { "$project": {
    "b2b": {
      "$map": {
        "input": "$b2b",
        "as": "b",
        "in": {
          "ctin": "$$b.ctin",
          "txval": { "$sum": "$$b.itms.itm_det.txval" },
          "iamt": { "$sum": "$$b.itms.itm_det.iamt" },
          "camt": { "$sum": "$$b.itms.itm_det.camt" },
          "samt": { "$sum": "$$b.itms.itm_det.samt" }
        }    
      }
    }
  }}
])

$map运算符基本上将数组作为输入,然后在每个项目上处理表达式以获得结果。在这种情况下,我们会从&#34;内部&#34;返回您想要总计的每个键的"ctin"值和$sum"itms"数组。

但是我会注意到,因为你并不是真的&#34;聚合&#34;我们几乎可以简单地迭代光标并简单地应用相同类型的转换。您通过简单地仅使用basic projection返回所需文档的键来保存所传输的数据,在这种情况下只是"b2b"数组和默认"_id"

这可以用任何语言完成,但对于通过MongoDB shell的JavaScript,这基本上使用相同的.map().reduce()数组方法

db.horrible.find({ },{ "b2b": 1 }).map(doc => {
  var obj = { "_id": doc._id };
  obj.b2b = doc.b2b.map(b => {
    var inner = { ctin: b.ctin };
    ["txval", "iamt", "camt", "samt"].forEach( k => 
      inner[k] = b.itms.map( e => e.itm_det[k] ).reduce((a,b) => a + b,0)
    );
    return inner;
  });
  return obj;
})

两种方法都产生相同的结果:

{
        "_id" : ObjectId("594717793468c33cb7523148"),
        "b2b" : [
                {
                        "ctin" : "32DS233TY781233",
                        "txval" : 31000.46,
                        "iamt" : 2600.4,
                        "camt" : 2600.5,
                        "samt" : 100
                },
                {
                        "ctin" : "32DS233TY781234",
                        "txval" : 14000.23,
                        "iamt" : 1200.2,
                        "camt" : 1300.25,
                        "samt" : 0
                },
                {
                        "ctin" : "32DS233TY781235",
                        "txval" : 16000.23,
                        "iamt" : 1300.2,
                        "camt" : 1400.25,
                        "samt" : 0
                }
        ]
}

答案 1 :(得分:0)

谢谢,我能够放松一下,但你的一个很漂亮。

db.GSTR2SAVE.aggregate([
                        { $project: { gstin: 1, "b2b.ctin": 1, 
"b2b.itms.itm_det.txval": 1, "b2b.itms.itm_det.iamt": 1, 
"b2b.itms.itm_det.camt": 1, "b2b.itms.itm_det.samt": 1,  
"b2b.itms.itm_det.csamt": 1 } },
                        { $unwind: { path: "$b2b" } },
                        { $unwind: { path: "$b2b.itms" } },
                        { $group: { _id: {ctin: "$b2b.ctin"}, t_txval: 
{$sum: "$b2b.itms.itm_det.txval"}, t_iamt: { $sum: 
"$b2b.itms.itm_det.iamt"}, t_camt: {$sum: "$b2b.itms.itm_det.camt"}, 
t_samt: {$sum: "$b2b.itms.itm_det.samt"}, t_csamt: {$sum: 
"$b2b.itms.itm_det.csamt"}} },
]).pretty()