am just learning how to build websites using react / nextjs, and I am structuring my data but unsure on the best practice, I am very new to the mongodb concept.
I stored comments I want to store likes for each post (how the schema would be ? and what will be added to post schema?)
it's the same as instagram likes on posts when userId exists i want it to decrease by one if not increase by one
posts schema:
import mongoose from 'mongoose';
const postSchema = new mongoose.Schema({
title: {
type: String,
trim: true,
required: true
},
contentEditor: {
type: String,
// trim: true,
},
author: String,
img: {
type: String
},
channelImg: {
type: String,
},
videoId:{
type: String,
},
filter:{
type: String,
},
// a blog post can have multiple comments, so it should be in a array.
// all comments info should be kept in this array of this blog post.
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}]
},{
timestamps: true, //updated at created at things
}
)
const Post = mongoose.models.Post || mongoose.model('Post', postSchema);
export default Post;
comments schema:
import mongoose from 'mongoose';
const { Schema } = mongoose;
const commentSchema = Schema(
{
content: { type: String, required: true },
post: { type: Schema.Types.ObjectId, ref: 'Post' },
author: { type: Schema.Types.ObjectId, ref: 'User' },
},
{ timestamps: true, collection: "comments", }
);
const Comments = mongoose.models.Comments || mongoose.model('Comments', commentSchema);
export default Comments;