Mongo aggregation structure with different number of keys with mutliple group by

I looked at a few answers and examples related to the above topic (including on SO), but could not find a working example for using the answers to my requirements:

I have a set of documents containing several dimensions (descriptive attributes) and metrics. I want to count the number of events (unique), a specific key value within a certain group of other keys. that is, how many players arrived from the country and speak a certain language.

Document structure:

{ "date": "2013-06-13T00:00:00.000Z" "Operating System": "Windows 7", "Browser": "Chrome", "Device": "Desktop/Laptop", "Country": "Afghanistan", "Language": "English", "Player": "91823781188577408" //This is a string value, the player id }, ... 

The requested result:

 { "Country": "Afghanistan", "Language": "English", "PlayerCount": 120 } 

In SQL, this would be something like:

 SELECT Country, Language, COUNT(DISTINCT PlayerCount) FROM Table GROUP BY Country, Language 

My last attempt at aggregation included $ project, $ group and $ relind of several types, unfortunately, no one worked, and there weren’t so many when adding them. Please note that after a decision based on the aggregation structure, and not with a map reduction, I get a solution.

Thank you very much.

+4
source share
1 answer

Try:

 db.YOUR_COLLECTION.aggregate([ { $group: { _id: { Country: "$Country", Language: "$Language" }, PlayerCount: {$sum: 1} } } ]); 

The country and language will be inside the "_id" field.

+9
source

Source: https://habr.com/ru/post/1490647/


All Articles