1路
评论嵌入在 Post 文档中:
{
"_id": ObjectId(12345),
"title": "Cat ate elephant!",
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean convallis pretium vulputate.",
"comments": [
{"name": "Cat", "body": "MEOW!"},
{"name": "Elephant", "body": "I was eaten by cat, lol!"},
{"name": "Human", "body": "I am hungry!"}
]
}
2种方法
帖子和评论之间的关系(在单独的文档中)。帖子有很多评论:
// POST //
{
"_id": ObjectId(12345),
"title": "Cat ate elephant!"
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean convallis pretium vulputate."
}
// Comments //
{
"_id": ObjectId(...),
"post_id": ObjectId(12345),
"name": "Cat",
"body": "MEOW!"
}
{
"_id": ObjectId(...),
"post_id": ObjectId(12345),
"name": "Elephant",
"body": "I was eaten by cat, lol!"
}
{
"_id": ObjectId(...),
"post_id": ObjectId(12345),
"name": "Human",
"body": "I am hungry!"
}
哪种方式更好?