[英]Mongoose many to many relation
I am beginner Node.JS and Mongoose programmer. 我是初学者Node.JS和Mongoose程序员。 So far I have created a very simple blog with 3 Models - Article, User, Categories and everything is working fine.
到目前为止,我已经创建了一个非常简单的博客,其中包含3个模型-文章,用户,类别,并且一切正常。 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() },
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.