title: Get multiple fields count from one query in MongoDB?
body: <p>I have a collection of events its structure is as follows :</p>
  
  <pre><code>{
      "_id" : ObjectId("537b3ff288f4ca2f471afcae"),
      "Name" : "PREMISES MAP DELETED",
      "ScreenName" : "AccessPointActivity",
      "Timestamp" : NumberLong("1392113758000"),
      "EventParams" : "null",
      "TracInfo" : {
              "ApplicationId" : "fa41f204bfc711e3b9f9c8cbb8c502c4",
              "DeviceId" : "2_1VafJVPu4yfdbMWO1XGROjK6iQZhq4hAVCQL837W",
              "UserId" : "pawan",
              "SessionId" : "a8UHE16mowNwNGyuLXbW",
              "WiFiAP" : "null",
              "WiFiStrength" : 0,
              "BluetoothID" : "null",
              "BluetoothStrength" : 0,
              "NetworkType" : "null",
              "NetworkSubType" : "null",
              "NetworkCarrier" : "Idea",
              "Age" : 43,
              "Gender" : "Female",
              "OSVersion" : "16",
              "Manufacturer" : "samsung",
              "Resolution" : "600*976",
              "Platform" : "Android",
              "Latitude" : 40.42,
              "Longitude" : -74,
              "City" : "Monmouth County",
              "CityLowerCase" : "monmouth county",
              "Country" : "United States",
              "CountryLowerCase" : "united states",
              "Region" : "New Jersey",
              "RegionLowerCase" : "new jersey",
              "Time_zone" : "null",
              "PinCode" : "07732",
              "Locale" : ", Paradise Trailer Park",
              "Accuracy" : 0,
              "Timestamp" : NumberLong("1392113758000")
      }
  }
  </code></pre>
  
  <p>their are many event on different screens.</p>
  
  <p>My expected output is as follows :</p>
  
  <pre><code>{
      ApplicationId:"fa41f204bfc711e3b9f9c8cbb8c502c4",
      EventName:"PREMISES MAP DELETED",
      Eventcount:300, 
      ScreenviewCount:20,
      DeviceCount:10, 
      UserCount:3 
  }
  </code></pre>
  
  <p>EventCount : It is count of EventName</p>
  
  <p>ScreenviewCount : It is the count of distinct screenName distinct per session</p>
  
  <p>DeviceCount : It is the count of distinct deviceId</p>
  
  <p>UserCount : It is the count of distinct userCount</p>
  
  <p>Their will be multiple event on multiple screens(ScreenName).</p>
  
  <p>Currently i' am using following approach :</p>
  
  <ol>
  <li><p>Using aggregation to get each event name and it count
  eg :</p>
  
  <pre><code>  {    
      _id:
      {
          ApplicationId:"fa41f204bfc711e3b9f9c8cbb8c502c4",
          EventName:"PREMISES MAP DELETED"    
      }
      EventCount:300    
  </code></pre>
  
  <p>}</p></li>
  <li><p>For each event name from above aggregation result I call following queries in while loop until aggregation output has documents:</p></li>
  </ol>
  
  <p>a) Distinct query using eventName from aggregation output for screenview count(on event collection).</p>
  
  <p>b) Distinct query eventName from aggregation output for device count(on event collection).</p>
  
  <p>c) Distinct query eventName from aggregation output for user count(on event collection).</p>
  
  <p>And the problem is its slow as it has 3 distinct queries on each result of aggregation output.</p>
  
  <p>Is their any way to do it in single aggregation call or something else.</p>
  
  <p>Thank you in advance!!!</p>