Skip to content

Instantly share code, notes, and snippets.

@afoninsky
Created January 13, 2017 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afoninsky/837021a1234b757b1709fd62f468168d to your computer and use it in GitHub Desktop.
Save afoninsky/837021a1234b757b1709fd62f468168d to your computer and use it in GitHub Desktop.
db.statistic.aggregate(
// Pipeline
[
// Stage 1
{
$group: {
_id: '$user._id',
total: { $sum: 1 }
}
},
// Stage 2
{
$sort: {
total: -1
}
},
// Stage 3
{
$limit: 100
},
// Stage 4
{
$lookup: {
"from" : "users",
"localField" : "_id",
"foreignField" : "_id",
"as" : "user"
}
},
// Stage 5
{
$unwind: {
path: '$user'
}
},
// Stage 6
{
$project: {
total: 1,
'user.username': 1,
'user.email': 1,
'user.firstName': 1,
'user.lastName': 1,
'user.provider': 1
}
},
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment