MongoDB中的计算字段

时间:2016-12-18 14:24:51

标签: mongodb aggregation-framework

我的文件如下。

{
    "name" : "MadhuA"+i,
    "lastname" : "MohanB"+i,
    "created" : new Date(),
    "Address" : [
        {
            "Home Address" : "St:123, Abcd road, cityA, stateB, Pin code123",
            "Work Address" : "St:134, xyz road, cityV, stateX, pin code456",
            "Billing Address" : "St:156, pppp road, cityZ, stateNm ,Pincode154"
        }
    ],
    "tariff" : [
        {
            "Service" : "Broadband",
            "Recurring" : "Monthly",
            "Fee" : 100
        }
    ],
    "charges" : [ {
            "charge_details" : [
                {
                    "start_date" : "01-JAN-2016",
                    "end_date" : "01-FEB-2016",
                    "charge_amount" : Math.floor(Math.random()*400),
                    "Status" : "Outstanding",
                    "Service" : "Broadband",
                    "charge_type" : "monthly"
                },
                {
                    "start_date" : "01-JAN-2016",
                    "end_date" : "01-FEB-2016",
                    "charge_amount" : Math.floor(Math.random()*400),
                    "Status" : "Outstanding",
                    "Service" : "VoIP",
                    "charge_type" : "monthly"
                }
            ] }
        ]
}

我希望使用费用字段中的所有charge_amounts填充总金额。只要在文档中添加了charge_amount,就应该相应地更改total_amount。 如果我们有这样的功能,请告诉我。

< 3

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您尝试添加属性totalAmount,以便:

  1. 包含charge_amount
  2. 的列表
  3. 文档更改时动态更改
  4. 如果是这种情况,最好的解决方案是使用带有视图的Mongo 3.4,编写如下内容:

    db.createView("totalAmount", "collection", [
      { 
       $addFields: {
           totalAmount:  "$charges.charge_details.charge_amount" 
         }
      },
      { 
       $addFields: {
           totalAmount:  { $arrayElemAt: ["$totalAmount",0] } 
         }
      },
    ])
    

    这里我假设您的收藏被称为"收集" 现在,您可以查询新视图,您将看到totalAmount:

    db.totalAmount.find().pretty 
    

    所示:

    {
        "_id" : ObjectId("5856c41fbd0535e2fb8fb2ff"),
        "name" : "MadhuA",
        "lastname" : "MohanB",
        "created" : ISODate("2016-12-18T17:15:11.797Z"),
        "Address" : [
            {
                "Home Address" : "St:123, Abcd road, cityA, stateB, Pin code123",
                "Work Address" : "St:134, xyz road, cityV, stateX, pin code456",
                "Billing Address" : "St:156, pppp road, cityZ, stateNm ,Pincode154"
            }
        ],
        "tariff" : [
            {
                "Service" : "Broadband",
                "Recurring" : "Monthly",
                "Fee" : 100
            }
        ],
        "charges" : [
            {
                "charge_details" : [
                    {
                        "start_date" : "01-JAN-2016",
                        "end_date" : "01-FEB-2016",
                        "charge_amount" : 37,
                        "Status" : "Outstanding",
                        "Service" : "Broadband",
                        "charge_type" : "monthly"
                    },
                    {
                        "start_date" : "01-JAN-2016",
                        "end_date" : "01-FEB-2016",
                        "charge_amount" : 398,
                        "Status" : "Outstanding",
                        "Service" : "VoIP",
                        "charge_type" : "monthly"
                    }
                ]
            }
        ],
        "totalAmount" : [
            37,
            398
        ]
    }
    

    此外,如果您将新文档添加到" collection"或修改任何文档视图(特别是totalAmount)将相应更改。

答案 1 :(得分:0)

出现此错误:

> db.totalAmount.find({"name" : "MadhuA4097899"}).pretty()
Error: error: {
    "ok" : 0,
    "errmsg" : "$arrayElemAt's first argument must be an array, but is int",
    "code" : 28689,
    "codeName" : "Location28689"
}