如何在Mongo(Studio 3T)上从json中的嵌套数组求和字段的值

时间:2018-10-15 06:03:09

标签: json mongodb studio3t

对于以下JSON,我想执行以下操作:

  1. 添加字段total的值,即(16 + 0 + 60 + 80)

  2. 执行上述操作,但按type进行过滤:

JSON

{ 
    "_id" : ObjectId("5bbdf40b59c9421e2ae588f5"), 
    "projectType" : "Residential", 
    "url" : "http://up-rera.in/Frm_View_Project_Details.aspx?id=10440", 
    "projectId" : "UPRERAPRJ10440", 
    "numberOfApartment" : NumberInt(156), 
    "apartmentType" : "Apartment/Flat/Shop", 
    "location2" : "Agra", 
    "location3" : "Agra", 
    "dateOfCompletion" : "24-03-2020", 
    "units" : [
        {
            "total" : NumberInt(16), 
            "type" : "4 BHK"
        }, 
        {
            "total" : NumberInt(0), 
            "type" : "OTHER"
        }, 
        {
            "total" : NumberInt(60), 
            "type" : "2 BHK"
        }, 
        {
            "total" : NumberInt(80), 
            "type" : "3 BHK"
        }
    ], 
    "state" : "uttar pradesh", 
    "projectName" : "APARNA PANCHSHEEL", 
    "projectStatus" : "Ongoing", 
    "projectStartDate" : "03-07-2013"
}

我正在使用Studio 3T。我正在使用的当前代码是

{ 
    _id: "$location2",
    totalUnits: { $sum: "$units.total" }    
}

任何帮助将不胜感激。

0 个答案:

没有答案