I am creating a MERN Stack Application which gives 'Points' to a User based on how many Posts, Comments and Likes they do.
I need to generate a LEADERBOARD from this, that Ranks all the Users (User with the most Posts, Comments & Likes) and also shows the sum count of Post, Comments & Likes.
Currently I have a userSchema as per below:
import mongoose from 'mongoose'
const { Schema } = mongoose
const userSchema = new Schema(
{
name: {
type: String,
trim: true,
required: true,
},
email: {
type: String,
trim: true,
required: true,
unique: true,
},
const postSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
title: {
type: String,
required: true,
},
comments: [
{
content: 'String',
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
},
],
likes: [
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
},
],
}
);
How do I list out in a LEADERBOARD all the Users and Rank them based on their TOTAL POINTS (sum of number of posts, comments & likes)?