【发布时间】:2020-07-17 15:41:52
【问题描述】:
发布收藏
Post :
_id : 001,
comments : [1,2,3] // ObjectId
评论收集
Comment:
{ _id : 1, message : "Bar" },
{ _id : 2, message : "Foo" },
{ _id : 3, message : "Bazz" }
我想删除一个帖子和该帖子上的所有 cmets。但是如何删除它的所有 cmets 呢?
更新
后模式
{
model: String,
stock: Number,
images: [String],
price: Number,
views: { type: Number, default: 1 },
commentsCount: { type: Number, default: 0 },
comments: [{ type: Schema.Types.ObjectId, ref: "postComments" }],
postedBy: { type: String, ref: "publisherInfo" }
},
postComments 架构
{
sender: String,
createAt: { type: Date, default: Date.now },
votes: { type: Number, default: 0 },
contain: String
},
【问题讨论】:
-
为了轻松应对这种情况,最好在子文档中保留一个父引用,我的意思是评论中的 postId。您可以将您的帖子和评论架构代码添加到问题中吗?
-
yaa。但我认为无论出于何种原因,它都是无用的数据(PostId),每个文档都会包含它。我认为有一些解决方案。所以我发布了这个问题。
-
你能添加帖子和评论架构吗?
-
当然...我也会添加这些架构