node-mongo-demo
node.js and mongodb demo
git clone https://9o.is/git/node-mongo-demo.git
commit b040827476552df931ccb5424b90cb86cdafe289 parent a0ce89cf1a1e45e3ec2384710a282e57a919a08d Author: Jul <jul@9o.is> Date: Tue, 28 Jan 2025 10:33:10 -0500 fix aggregate pipeline to add win field Diffstat:
| M | backend/scripts/populate_database.js | | | 6 | +++--- |
| M | backend/src/queries/lucky7-leaderboard.js | | | 191 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
| M | readme.md | | | 7 | ++++++- |
3 files changed, 113 insertions(+), 91 deletions(-)
diff --git a/backend/scripts/populate_database.js b/backend/scripts/populate_database.js @@ -7,8 +7,8 @@ import Lucky7Bet from '../src/models/lucky7-bet.js'; const startDate = new Date('2024-01-01') const roll = () => Math.round(Math.random()) - ? { win: true, lucky: true, roll: [3,4] } - : { win: false, lucky: true, roll: [1,1] }; + ? { lucky: true, roll: [3,4] } + : { lucky: true, roll: [1,1] }; const generateBets = (length, users) => users.flatMap(({ _id }) => @@ -30,7 +30,7 @@ const generateUsers = length => (async function() { try { - await mongoose.connect(process.env.MONGODB_URL) + await mongoose.connect(process.env.MONGODB_URL); await User.init(); await Lucky7Bet.init(); diff --git a/backend/src/queries/lucky7-leaderboard.js b/backend/src/queries/lucky7-leaderboard.js @@ -4,102 +4,119 @@ export const TOP_10_STREAKS = [{ rollAt: -1, } }, - { - $group: { - _id: '$userId', - bets: { - $push: '$$ROOT' - } +{ + $addFields: { + win: { + $cond: [ + { + $eq: [ + { + $sum: "$roll" + }, + 7 + ] + }, + true, + false + ] } - }, - { - $project: { - result: { - $reduce: { - input: '$bets', - initialValue: { - streaks: [], - currentStreak: 0 - }, - in: { - streaks: { - $cond: [{ - $eq: ['$$this.win', true] - }, - '$$value.streaks', - { - $cond: [{ - $gt: [ - '$$value.currentStreak', - 0 - ] - }, - { - $concatArrays: [ - '$$value.streaks', - [ - '$$value.currentStreak' - ] - ] - }, - '$$value.streaks' - ] - } - ] + } +}, +{ + $group: { + _id: '$userId', + bets: { + $push: '$$ROOT' + } + } +}, +{ + $project: { + result: { + $reduce: { + input: '$bets', + initialValue: { + streaks: [], + currentStreak: 0 + }, + in: { + streaks: { + $cond: [{ + $eq: ['$$this.win', true] }, - currentStreak: { - $cond: [{ - $eq: ['$$this.win', true] - }, - { - $add: [ + '$$value.streaks', + { + $cond: [{ + $gt: [ '$$value.currentStreak', - 1 + 0 ] }, - 0 - ] - } + { + $concatArrays: [ + '$$value.streaks', + [ + '$$value.currentStreak' + ] + ] + }, + '$$value.streaks' + ] + } + ] + }, + currentStreak: { + $cond: [{ + $eq: ['$$this.win', true] + }, + { + $add: [ + '$$value.currentStreak', + 1 + ] + }, + 0 + ] } } } } - }, - { - $project: { - streak: { - $cond: [{ - $gt: ['$result.currentStreak', 0] + } +}, +{ + $project: { + streak: { + $cond: [{ + $gt: ['$result.currentStreak', 0] + }, + { + $concatArrays: [ + '$result.streaks', + ['$result.currentStreak'] + ] }, - { - $concatArrays: [ - '$result.streaks', - ['$result.currentStreak'] - ] - }, - '$result.streaks' - ] - } - } - }, - { - $unwind: '$streak' - }, - { - $sort: { - streak: -1 + '$result.streaks' + ] } - }, - { - $limit: 10 - }, - { - $lookup: { - from: 'users', - localField: '_id', - foreignField: '_id', - as: 'users' - } - }, -]; + } +}, +{ + $unwind: '$streak' +}, +{ + $sort: { + streak: -1 + } +}, +{ + $limit: 10 +}, +{ + $lookup: { + from: 'users', + localField: '_id', + foreignField: '_id', + as: 'users' + } +}]; diff --git a/readme.md b/readme.md @@ -41,7 +41,12 @@ Now in the backend folder. Run the start command in a separate terminal. npm run start ``` -The backend is now up and running. +The backend is now up and running. Feel free to use the populate script to generate +users and bets to inspect the leaderboard endpoint. + +```bash +MONGODB_URL="<INPUT URL>" node scripts/populate_database.js +``` ---