0

I need the sum of ldl_xml_count for data array but I am unable to get the sum value for that field.

Design data

db.commination.findOne()
{
    "_id" : ObjectId("5530dc5272b7e56b11497ce8"),
    "data" : [
        {
            "ldl_date" : ISODate("2015-04-08T18:30:00Z")
        },
        {
            "ldl_mmo_id" : 5
        },
        {
            "ldl_xml_count" : 1
        }
    ],
    "masterid" : [
        {
            "ldl_date" : ISODate("2015-04-08T18:30:00Z")
        },
        {
            "ldl_mmo_id" : 5
        },
        {
            "ldl_xml_count" : 1
        },
        {
            "ldl_master_info_id" : 11
        }
    ],
    "pubid" : [
        {
            "ldl_publication_info_id" : 41616,
            "ldl_xml_info_id" : 37437691
        }
    ],
    "details" : [
        {
            "ldl_id" : 54261629,
            "ldl_distribution_id" : 3289,
            "ldl_local_flag" : 1,
            "ldl_ftp_flag" : 0,
            "ldl_time" : ISODate("2015-04-09T01:06:46Z")
        }
    ]
}

mongo query

db.commination.aggregate([
    {
        $match:{
            "data.ldl_date": {$gte: new Date("2015-01-08T18:30:00Z")}
        }
    },
    {
        $unwind :"$data"
    },
    { 
        $group: {
            _id:"$data.ldl_mmo_id", 
            total: { $sum: "$data.ldl_xml_count" } 
        } 
     }
])

Output

{ "_id" : 1, "total" : 0 }

{ "_id" : 3, "total" : 0 }

{ "_id" : 4, "total" : 0 }

{ "_id" : 5, "total" : 0 }

{ "_id" : 2, "total" : 0 }

{ "_id" : null, "total" : 500000 }

I need the sum for id wise.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 2
    possible duplicate of [Sum in nested document MongoDB](http://stackoverflow.com/questions/14570577/sum-in-nested-document-mongodb) – Neo-coder Apr 17 '15 at 11:06
  • 2
    may be you asked same question here also http://stackoverflow.com/questions/29647609/how-to-access-embeded-documents-using-aggregation-function – Neo-coder Apr 17 '15 at 11:07

0 Answers0