我如何在模式猫鼬中创建 parent 包含 children 评论的评论
how Can I create parent comment which contain children comments in schema mongoose
思路是关于一条评论包含children条评论,是关于一条评论包含回复。为此,我创建了这样的评论模式:
import mongoose from 'mongoose';
//to connect to our database
import db from '../db/connectDB.js'
const Schema = mongoose.Schema // Define Schema method= mongoose;
const CommentSchema = new Schema({
id_parent : {
type: Number,
required: true,
},
user_name: {
type: String,
},
content: {
type: String,
},
created_date: {
type: Date,
default: Date.now,
},
counter_like: {
type: Number,
required:false,
},
status : {
type: String,
},
});
export default mongoose.model('Comment', CommentSchema);
然后我的parent评论架构是这样的:
import mongoose from 'mongoose';
//to connect to our database
import db from '../db/connectDB.js'
const SchemaParent = mongoose.Schema // Define Schema method= mongoose;
const ParentCommentSchema = new Schema({
parent_comment:{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
},
children_comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}]
});
export default mongoose.model('ParentComment', ParentCommentSchema);
好像不干净,我不想每次都插入child评论我想直接在parent评论里插入,我没看懂,任何帮助
您可以简单地在评论中引用 parent ID
const commentSchema = new Schema({
_id,
parentId : { type: mongoose.Schema.Types.ObjectId, ref: 'Comments' },
user: { type: mongoose.Schema.Types.ObjectId, ref 'Users'},
[...]
});
通过这种方式,您可以.find({parentId: commentId})
找到所有可能成为 commentId
(parent) 的“答案”的评论。你将有一个 Comment
的数组
否则你可以直接嵌入彼此的评论。
const commentSchema = new Schema({
_id,
user: { type: mongoose.Schema.Types.ObjectId, ref 'Users'}
});
commentSchema.add({comments: {type: [commentSchema], default: undefined})
你会有这样的东西:
comment {
_id: 'qzrqrsrt',
user: '1'
content : "What do you guys think ?"
comments : [
{
_id: 'qzrqzrqzr',
user: '2',
content: 'Ahah nice',
comments: [
{
_id: 'qiorqzpçrq',
user: '1',
content: 'Thanks'
}
]
},
{
_id: 'dryhsertse',
user: '2'
content: "I'll use it"
}
]
}
但是要找到你想添加答案的评论会有点困难
思路是关于一条评论包含children条评论,是关于一条评论包含回复。为此,我创建了这样的评论模式:
import mongoose from 'mongoose';
//to connect to our database
import db from '../db/connectDB.js'
const Schema = mongoose.Schema // Define Schema method= mongoose;
const CommentSchema = new Schema({
id_parent : {
type: Number,
required: true,
},
user_name: {
type: String,
},
content: {
type: String,
},
created_date: {
type: Date,
default: Date.now,
},
counter_like: {
type: Number,
required:false,
},
status : {
type: String,
},
});
export default mongoose.model('Comment', CommentSchema);
然后我的parent评论架构是这样的:
import mongoose from 'mongoose';
//to connect to our database
import db from '../db/connectDB.js'
const SchemaParent = mongoose.Schema // Define Schema method= mongoose;
const ParentCommentSchema = new Schema({
parent_comment:{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
},
children_comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}]
});
export default mongoose.model('ParentComment', ParentCommentSchema);
好像不干净,我不想每次都插入child评论我想直接在parent评论里插入,我没看懂,任何帮助
您可以简单地在评论中引用 parent ID
const commentSchema = new Schema({
_id,
parentId : { type: mongoose.Schema.Types.ObjectId, ref: 'Comments' },
user: { type: mongoose.Schema.Types.ObjectId, ref 'Users'},
[...]
});
通过这种方式,您可以.find({parentId: commentId})
找到所有可能成为 commentId
(parent) 的“答案”的评论。你将有一个 Comment
否则你可以直接嵌入彼此的评论。
const commentSchema = new Schema({
_id,
user: { type: mongoose.Schema.Types.ObjectId, ref 'Users'}
});
commentSchema.add({comments: {type: [commentSchema], default: undefined})
你会有这样的东西:
comment {
_id: 'qzrqrsrt',
user: '1'
content : "What do you guys think ?"
comments : [
{
_id: 'qzrqzrqzr',
user: '2',
content: 'Ahah nice',
comments: [
{
_id: 'qiorqzpçrq',
user: '1',
content: 'Thanks'
}
]
},
{
_id: 'dryhsertse',
user: '2'
content: "I'll use it"
}
]
}
但是要找到你想添加答案的评论会有点困难