For your 1st question Is there a way to count the total number of documents which "belong" to a userId of xxx-12345, where type is 1?
, below is the solution:
db.impressions.aggregate({
$match: {
userId: 'xxx-12345',
type: 1
}
},
{
$group: { _id: null, count: { $sum: 1 } }
});
To get the solution in format you specified (In the above case, I'd want to see a result like { '1':3, '2':1 }.
), use below code:
db.impressions.aggregate({
$match: {
userId: 'xxx-12345',
}
},
{
$group: { _id: '$type', totalImpressions: { $sum: 1 } }
});