I have a MongoDb collection called UserRecords. It stores all records for our users, where each user can have many records.
I am trying to calculate some basic statistics related to the number of records per user.
Specifically I would like the mean, median and mode of the number of records per user.
So far I have a query that groups all UserRecords by User_Id (uid) and counts the number of UserRecords for each user.
db.UserRecords.aggregate([
{$group:
{_id:{"uid":"$uid"},
count:{$sum:1}}}
])
My query produces results that look like the following:
{
"_id" : {
"uid" : UUID("f22880a8-94d2-4524-a974-a2e500e2c2a2")
},
"count" : 100
}
{
"_id" : {
"uid" : UUID("1b3a3b81-d107-4345-8df5-a5ef00e23598")
},
"count" : 200
}
I would need my query to calculate the average of all the "count" values. For example, suppose the above results were the only 2 groups produced. I would need my query to do (100 + 200) / 2 = 150 and print that value of 150 to the console.
Does anyone know what I can add to my query to accomplish this?
*Edit, I would ideally like my result structure to be:
{
"mean": 1000,
"median": 850
"mode": 900
}