I have a JSON collection as an array. I would like to group by three fields within the collection and then return the result along with the count of the matching documents. The example below will hopefully make it clearer.
The JSON document collection returned:
[
{
_id: 1,
browser: 'chrome',
ipAddress: '222.111.111.0',
uri: 'example1.com'
},
{
_id: 2,
browser: 'chrome',
ipAddress: '222.111.111.0',
uri: 'example1.com'
},
{
_id: 3,
browser: 'opera',
ipAddress: '222.0.888.0',
uri: 'example1.com'
},
{
_id: 4,
browser: 'chrome',
ipAddress: '222.111.222.0',
uri: 'sample1.com'
},
{
_id: 5,
browser: 'chrome',
ipAddress: '222.111.222.0',
uri: 'sample1.com'
},
{
_id: 6,
browser: 'chrome',
ipAddress: '222.111.222.0',
uri: 'sample1.com'
},
{
_id: 7,
browser: 'opera',
ipAddress: '222.111.222.0',
uri: 'sample1.com'
}
]
Should perform a grouping on browser, ipAddress and uri and then return the grouped result along with a count as per below (I checked a few times so I hope my numbers below add up to the instances of each combination above!).
[
{
browser: 'chrome',
ipAddress: '222.111.111.0',
uri: 'example1.com',
count: 2
},
{
browser: 'opera',
ipAddress: '222.0.888.0',
uri: 'example1.com',
count: 1
},
{
browser: 'chrome',
ipAddress: '222.111.222.0',
uri: 'sample1.com',
count: 3
},
browser: 'opera',
ipAddress: '222.111.222.0',
uri: 'sample1.com',
count: 1
]
I get that this should be easily doable using map / reduce but I cannot seem to get my confused brain around how to do this!
Thanks in advance.