在 MongoDB 中存储嵌套集(如评论树)的最佳实践是什么?
我的意思是,每条评论都可以有父评论和子评论(答案)。
像这样存储它们:
{
title: "Hello",
body: "Please comment me!",
comments: [
{
author: "Peter",
text: "Hi there",
answers: [
{
author: "Peter",
text: "Hi there",
answers: [
{ author: "Ivan", text: "Hi there" },
{ author: "Nicholas", text: "Hi there" }
]
},
{ author: "Ivan", text: "Hi there" },
{ author: "Nicholas", text: "Hi there" },
]
},
{ author: "Ivan", text: "Hi there" },
{ author: "Nicholas", text: "Hi there" },
]
}
这并不酷,因为我们不能,例如,在没有 map/reduce 的情况下要求“所有由 Peter 评论的帖子”。