Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- db.getCollection('versus-challenges').aggregate([{
- $match: {
- type: {
- $in: ['public','private']
- }
- }
- }, { // "uuids" is an array of uuids, so we split it
- $unwind: '$uuids'
- }, { // Index by uuid+type, and add the challenges ID in an array
- $group: {
- _id: {
- uuid: '$uuids',
- type: '$type'
- },
- challenges: {
- $addToSet: '$id'
- }
- }
- }, { // Split the array of challenge ID into multiple document to be able to $group again on them
- $unwind: '$challenges'
- },{ // Group by uuid & type, count how many of each
- $group: {
- _id: {
- uuid: '$_id.uuid',
- type: '$_id.type'
- },
- challenges: {
- $sum: 1
- }
- }
- },{ // Re-assemble into a single document per uuid
- $group: {
- _id: '$_id.uuid',
- types_played: {
- $sum: 1
- },
- played_total: {
- $sum: '$challenges'
- },
- stats: {
- $push: {
- type: '$_id.type',
- played: '$challenges'
- }
- }
- }
- }, { // Sort to get the users who played the most on top
- $sort: {
- played_total: -1
- }
- }])
Advertisement
Add Comment
Please, Sign In to add comment