Skip to content

Instantly share code, notes, and snippets.

@Yooooomi
Last active April 3, 2019 17:40
Show Gist options
  • Save Yooooomi/d3059c1e8c5d0c6ef060b53b662ccb01 to your computer and use it in GitHub Desktop.
Save Yooooomi/d3059c1e8c5d0c6ef060b53b662ccb01 to your computer and use it in GitHub Desktop.
return await Competition.aggregate([
{
$match: {
$expr: {
$eq: ['$_id', { $toObjectId: compId }],
}
}
},
{ $unwind: '$steps' },
{
$lookup: {
from: 'steps',
as: 'stepsObject',
localField: 'steps',
foreignField: '_id',
}
},
{ $unwind: '$stepsObject' },
{
$project: {
_id: 1,
stepsObject: 1,
}
},
{
$lookup: {
from: 'matches',
as: 'matchesObject',
let: { otherid: '$stepsObject.matchs' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $in: ['$_id', '$$otherid'] },
{ $ne: ['$localScore', -1] },
]
}
}
}
],
},
},
{
$project: {
_id: '$stepsObject._id',
matchesObject: 1,
}
},
{ $unwind: '$matchesObject' },
{
$lookup: {
from: 'users',
as: 'users',
pipeline: [
{
$project: {
_id: 1,
username: 1,
pronos: 1,
}
}
],
}
},
{ $unwind: '$users' },
{
$lookup: {
from: 'pronos',
as: 'pronosObject',
let: { matchId: '$matchesObject._id', ourPronos: '$users.pronos' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$$matchId', '$match'] },
{ $in: ['$_id', '$$ourPronos'] },
]
}
}
}
]
}
},
{
$project: {
_id: '$_id',
user_id: '$users._id',
match: '$matchesObject',
prono: { $arrayElemAt: ['$pronosObject', 0] },
}
},
{
$addFields: {
stats: {
$switch: {
branches: [
{
case: {
$and: [
{ $eq: ['$prono.local', '$match.localScore'] },
{ $eq: ['$prono.guest', '$match.guestScore'] },
],
},
then: { points: { $multiply: [3, '$prono.coeff'] }, sj: 1, bt: 0, failed: 0 },
},
{
case: {
$and: [
{ $lt: [{ $subtract: ['$prono.local', '$prono.guest'] }, 0] },
{ $lt: [{ $subtract: ['$match.localScore', '$match.guestScore'] }, 0] }
]
},
then: { points: { $multiply: [1, '$prono.coeff'] }, sj: 0, bt: 1, failed: 0 },
},
{
case: {
$and: [
{ $gt: [{ $subtract: ['$prono.local', '$prono.guest'] }, 0] },
{ $gt: [{ $subtract: ['$match.localScore', '$match.guestScore'] }, 0] }
]
},
then: { points: { $multiply: [1, '$prono.coeff'] }, sj: 0, bt: 1, failed: 0 },
},
{
case: {
$and: [
{ $eq: [{ $subtract: ['$prono.local', '$prono.guest'] }, 0] },
{ $eq: [{ $subtract: ['$match.localScore', '$match.guestScore'] }, 0] }
]
},
then: { points: { $multiply: [1, '$prono.coeff'] }, sj: 0, bt: 1, failed: 0 },
},
],
default: { points: { $multiply: [-1, '$prono.coeff'] }, sj: 0, bt: 0, failed: 1 },
}
}
}
},
{
$group: {
_id: '$user_id',
points: { $sum: '$stats.points' },
sj: { $sum: '$stats.sj' },
bt: { $sum: '$stats.bt' },
failed: { $sum: '$stats.failed' },
pronos: { $sum: 1 },
totalCoeff: { $sum: '$prono.coeff' },
goals: { $sum: { $add: ['$prono.local', '$prono.guest'] } }
}
},
{
$sort: {
'stats.points': 1,
}
},
{
$limit: 50,
},
{
$lookup: {
from: 'users',
as: 'user',
localField: '_id',
foreignField: '_id',
}
},
{ $unwind: '$user' },
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment