I have documents like:

{
    "platform":"android",
    "install_date":20151029
}
  1. platform - can have one value from [android|ios|kindle|facebook ] .
  2. install_date - there are many install_dates

There are also many fields.

Aim : I am calculating installs per platform on particular date.

So I am using group by in aggregation framework and make counts by platform. Document should look like like:

{
  "install_date":20151029,
  "platform" : {
   "android":1000,
   "ios": 2000,
   "facebook":1500
  }
}

I have done like:

db.collection.aggregate([
  { 
      $group: { 
          _id: { platform: "$platform",install_date:"$install_date"},  
            count: { "$sum": 1 } 
        }
  },
  { 
      $group: { 
          _id: { install_date:"$_id.install_date"},
          platform: { $push :  {platform :"$_id.platform", count:"$count" } }  
      }
  },
  { 
     $project : { _id: 0, install_date: "$_id.install_date", platform: 1 } 
  }     
]) 

which Gives document like:

{
    "platform": [
        {
            "platform": "facebook",
            "count": 1500
        },
        {
            "platform": "ios",
            "count": 2000
        },
        {
            "platform": "android",
            "count": 1000
        }
    ],
    "install_date": 20151027
}

Problem:

Projecting array to single object as "platform"

Solution 1

With MongoDb 3.4 and newer, you can leverage the use of $arrayToObject operator to get the desired result. You would need to run the following aggregate pipeline:

db.collection.aggregate([
    { "$group": {
        "_id": {  
            "date": "$install_date",  
            "platform": { "$toLower": "$platform" }
        },
        "count": { "$sum": 1 }
    } },
    { "$group": {
        "_id": "$_id.date",
        "counts": {
            "$push": {
                "k": "$_id.platform",
                "v": "$count"
            }
        }
    } },
    {  "$addFields": {
        "install_date": "$_id", 
        "platform": { "$arrayToObject": "$counts" }
    }  },
    { "$project": { "counts": 0, "_id": 0 } } 
])

For older versions, take advantage of the $cond operator in the $group pipeline step to evaluate the counts based on the platform field value, something like the following:

db.collection.aggregate([    
    { "$group": { 
        "_id": "$install_date",             
        "android_count": {
            "$sum": {
                "$cond": [ { "$eq": [ "$platform", "android" ] }, 1, 0 ]
            }
        },
        "ios_count": {
            "$sum": {
                "$cond": [ { "$eq": [ "$platform", "ios" ] }, 1, 0 ]
            }
        },
        "facebook_count": {
            "$sum": {
                "$cond": [ { "$eq": [ "$platform", "facebook" ] }, 1, 0 ]
            }
        },
        "kindle_count": {
            "$sum": {
                "$cond": [ { "$eq": [ "$platform", "kindle" ] }, 1, 0 ]
            }
        } 
    } },
    { "$project": {
        "_id": 0, "install_date": "$_id",            
        "platform": {
            "android": "$android_count",
            "ios": "$ios_count",
            "facebook": "$facebook_count",
            "kindle": "$kindle_count"
        }
    } }
])

In the above, $cond takes a logical condition as it's first argument (if) and then returns the second argument where the evaluation is true (then) or the third argument where false (else). This makes true/false returns into 1 and 0 to feed to $sum respectively.

So for example, if { "$eq": [ "$platform", "facebook" ] }, is true then the expression will evaluate to { $sum: 1 } else it will be { $sum: 0 }