MongoDB relationships: embed or reference?
I'm new to MongoDB--coming from a relational database background. I want to design a question structure with some comments, but I don't know which relationship to use for comments: embed
or reference
?
A question with some comments, like stackoverflow, would have a structure like this:
Question
title = 'aaa'
content = bbb'
comments = ???
At first, I want to use embeded comments (I think embed
is recommended in MongoDB), like this:
Question
title = 'aaa'
content = 'bbb'
comments = [ { content = 'xxx', createdAt = 'yyy'},
{ content = 'xxx', createdAt = 'yyy'},
{ content = 'xxx', createdAt = 'yyy'} ]
It clear, but I'm worried about this case: If I want to edit a specified comment, how do I get its content and its question? There is no _id
to let me find one, nor question_ref
to let me find its question. (I'm so newbie, that I don't know if there's any way to do this without _id
and question_ref
.)
Do I have to use ref
not embed
? Then I have to create a new collection for comments?