Skip to content

Instantly share code, notes, and snippets.

@dglowinski
Created February 2, 2018 09:21
Show Gist options
  • Save dglowinski/80feed0e8d51ec81040c143ab825967d to your computer and use it in GitHub Desktop.
Save dglowinski/80feed0e8d51ec81040c143ab825967d to your computer and use it in GitHub Desktop.
Account.aggregate([
{$unwind: {path: "$tokens"}},
{$match:{ "tokens.tokenId": tokenId }},
{$project: {
address: 1,
balance:{
$reduce: {
input: "$tokens.transactions",
initialValue: 0,
in: { $add:["$$value", "$$this.amount" ]}
}
}}},
{$group: {
_id: 0,
count: {
$sum: 1
},
document: {
$push: "$$ROOT"
}
}},
{$unwind: "$document"},
{$sort: {"document.balance": -1}},
{$skip: (page-1)*limit},
{$limit: limit}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment