const mongoose = require("mongoose"); const CommentSchema = new mongoose.Schema( { user: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true, index: true, }, post: { type: mongoose.Schema.Types.ObjectId, ref: "Post", required: true, index: true, }, content: { type: String, required: true, trim: true, maxlength: 500, }, }, { timestamps: true, }, ); // Compound index for efficient querying of comments by post CommentSchema.index({ post: 1, createdAt: -1 }); module.exports = mongoose.model("Comment", CommentSchema);