I am beginner Node.JS and Mongoose programmer. So far I have created a very simple blog with 3 Models - Article, User, Categories and everything is working fine. Now I want to create comments. So I want every user to be able to have manny comments and every articles also to be able to have manny comments. How can I do that and how can I represent logic in the controller to interact with each other:
let articleSchema = mongoose.Schema (
{
author: {type: ObjectId, ref: 'User'},
title: {type: String, required: true },
content: {type: String, required: true },
category: {type: ObjectId, ref: 'Category', required: true},
date: {type: Date, default: Date.now() },
}
);
let userSchema = mongoose.Schema(
{
email: {type: String, required: true, unique: true},
passwordHash: {type: String, required: true},
fullName: {type: String, required: true},
salt: {type: String, required: true},
articles: [{type: ObjectId, ref: 'Article'}],
roles: [{type: ObjectId, ref: 'Role'}]
}
);
let categorySchema = mongoose.Schema (
{
name: {type: String, required:true},
articles: [{type: ObjectId, ref: 'Article'}]
}
);
Create Comment schema which will reference Author and Article:
let commenSchema = mongoose.Schema (
{
author: {type: ObjectId, ref: 'User', required: true},
content: {type: String, required: true },
article: {type: ObjectId, ref: 'Article', required: true},
date: {type: Date, default: Date.now() },
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.