$unwind two nested array in embedded document and use $group

612 views
Skip to first unread message

kanishk puri

unread,
Jun 29, 2016, 11:58:22 AM6/29/16
to mongodb-user
{
    "_id" : ObjectId("576e12ff7f5d9c17b86f577a"),
    "CustomerName" : "GIO_",
    "CustomerId" : "1485",
    "ReportDate" : ISODate("2016-05-31T18:30:00.000Z"),
    "ReportWeek" : 22,
    "ReportMonth" : 6,
    "ReportYear" : "2016",
    "LoadDate" : ISODate("2016-06-25T05:13:35.754Z"),
    "UpdateDateTime" : ISODate("2016-06-25T05:13:35.754Z"),
    "ActiveFlag" : true,
    "Display" : [ 
        {
           
            "Impressions" : 0,
      
          
        }
      
    ], "Social" : [ 
        {
            "
            "SocialImpressions" : 02,
            
          
        }
      
    ]
}


i want the sum of Impressions and SocialImpressions

Asya Kamsky

unread,
Jul 1, 2016, 1:52:23 AM7/1/16
to mongodb-user
I don't know what you want to group everything by, but for each individual document you get the sum like this:

{$project:{sumOfDisplayAndSocial:{$add:[{$sum:"$Display.Impressions"},{$sum:"$Social.SocialImpressions"}]}}}

So if you wanted sum of these two across entire collection you'd group by null and do this:

db.collection.aggregate( {$group: { _id: null, sumOfAll:{$sum: {$add:[{$sum:"$Display.Impressions"},{$sum:"$Social.SocialImpressions"}]}}}})

Asya


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/dc00a667-e981-4a84-a3d9-5b86b122b33e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers
Reply all
Reply to author
Forward
0 new messages