【发布时间】:2020-12-21 06:09:51
【问题描述】:
我有 1 个博客帖子模型:
const blogSchema= new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
name: {
type: String,
required: true,
max: 50,
min: 6,
},
description: {
type: String,
required: true,
max: 1024,
min: 25,
},
date: {
type: Date,
default: Date.now,
},
comment: [commentSchema],
});
这里的重要部分是最后一个字段comment。为此,我有另一个架构:
const commentSchema = new mongoose.Schema({
date: {
type: Date,
default: Date.now,
},
comment: {
type: String,
max: 1024,
min: 5,
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
});
通常在创建帖子时,可以添加 cmets。在单独的文件 User.js 中,我有用户模型:
const userSchema = new mongoose.Schema({
name: {
type: String,
unique: true,
required: true,
max: 255,
min: 6,
},
email: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
min: 6,
},
date: {
type: Date,
default: Date.now,
},
});
我如何将这些连接起来一起工作。我有所有帖子都可见的端点(无论是哪个用户提供的)。我希望 1 个用户能够评论另一个用户的帖子,并且他的名字出现在它下面。
所以任何想法我可以如何创建:
- 将 cmets 存储在 blogSchema 的评论字段中的端点
- 用户将被记录在commentSchema中
【问题讨论】:
标签: node.js mongodb mongoose schema endpoint