0

I'm new to robmongo and I received an assignment to write some queries.

let say I have a collection that each key has some values for example value of "userId" and value of "deviceModel". I need to write a query that shows for each device model how many users has this device. this is what I got so far:

db.device_data.aggregate([ {"$group" : {_id:"$data.deviceModel", count:{$sum:1}}}])

The problem is that this aggregate for each device the number of keys it appears.

{
"_id" : { "$binary" : "AN6GmE7Thi+Sd/dpLRjIilgsV/4AAAg=", "$type" : "00" },
"auditVersion" : "1.0",
"currentTime" : NumberLong(1479301118381),
"data" : {
    "deviceDesign" : "bullhead",
    "loginType" : "GOOGLE",
    "source" : "SDKLoader",
    "systemUptimeMillis" : 137652880.0,
    "simCountryIso" : "il",
    "networkOperatorName" : "Cellcom",
    "hasPhonePermission" : true,
    "deviceIdentifier" : "353627074839559",
    "sdkVersion" : "0.7.939.2016-11-14.masterDev",
    "brand" : "google",
    "osVersion" : "7.0",
    "osVersionIncremental" : "3239497",
    "deviceModel" : "Nexus 5X",
    "deviceSDKVersion" : 24.0,
    "manufacturer" : "LGE",
    "sdkShortBuildDate" : "2016-11-14",
    "sdkFullBuildDate" : "Mon Nov 14 22:16:40 IST 2016",
    "product" : "bullhead"
},
"timezone" : "Asia/Jerusalem",
"collectionAlias" : "DEVICE_DATA",
"shortDate" : 17121,
"userId" : "00DE86984ED3862F9277F7692D18C88A@1927cc81cfcf7a467e9d4f4ac7a1534b"}

this is an example of how one key locks like.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
yairabr
  • 101
  • 1
  • 1
  • 6

1 Answers1

1

The below query should give you distinct count of userId for a deviceModel. I meant if a same userId present for a deviceModel multiple items, it will be counted only once.

db.collection.aggregate([ {"$group" : {_id:"$data.deviceModel", userIds:{$addToSet: "$userId"}}
},
{
    $unwind:"$userIds"
},
{
    $group: { _id: "$_id", userIdCount: { $sum:1} }
}])

Unwind:-

Deconstructs an array field from the input documents to output a document for each element.

In the above solution, it deconstructs the userId array formed on the first pipeline.

addToSet:-

Returns an array of all unique values that results from applying an expression to each document in a group of documents that share the same group by key.

This function ensures that only unique values are added to an array. In the above case, the userId is added to an array in the first pipeline.

notionquest
  • 37,595
  • 6
  • 111
  • 105