I need a query that takes multiple ‘companyID’s’ and return the count for each company.
Currently this query only does this for one companyID and it does not return the id but just ‘null’ like show below.
I understand that I can use the ‘in’ operator for multiple companyID’s but not sure how I would go about having the query return multiple objects of counts for each companyID.
db.getCollection('reg').aggregate([{
'$match': {
'$and': [
{
'companyID': 11
},
{
'created': {
'$gte': 1556726597
}
},
{
'created': {
$lt: 1580572997
}
}
]
}
},
{
'$project': {
'testID': 1,
}
},
{
'$group': {
'_id': '$testID',
'registrationsCount': { '$sum': 1 },
},
},
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
])
The result below
{
"_id" : null,
"count" : 10.0
}
Schema below
{
"_id" : NumberLong(1),
"appUserID" : NumberLong(4294967295),
"companyID" : NumberLong(5),
"created" : NumberLong(1372625588),
"testID" : NumberLong(11),
"isCheckIn" : true,
"lastModified" : NumberLong(1372625588),
"source" : "upload",
"timeArrived" : NumberLong(1343062512),
}