Let's say I am building a discussion forum using Node.js, and mongoose. A user can have multiple forums, and a forum can have multiple comments. A user can also invite other users to join a forum too. Thus, my question is about the model design either using reference or embedded document !
If I go with embedded document, It would look like:
var Comment = new Schema({ ... });
var Forum = new Schema({
title: {type: String},
content: {type: String},
comments: [Comment],
attendees: [User]
});
var User = new Schema({
name: {type: String},
email: {type: String},
forums: [Forum]
});
var Account = mongoose.model('Account', User);
Using the above design, I struggled with: when a user adds a comment to a forum, and that forum is in my forums, I don't think I would be able to get update of a new comment in my forum list. Do I ? Do you know how to get the embedded document to work in this case?
Thus, I was thinking of using reference in mongoose. In this case, I will have two collections: Account, and Forum. Adding a new comment to a forum is not a problem in this case. Am I right?
Would reference be better than embedded document for this app?
Thanks in advance,
Embedded documents are an efficient and clean way to store related data, especially data that's regularly accessed together. In general, when designing schemas for MongoDB, you should prefer embedding by default, and use references and application-side or database-side joins only when they're worthwhile.
MongoDB provides you a cool feature which is known as Embedded or Nested Document. Embedded document or nested documents are those types of documents which contain a document inside another document.
The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _id s we store here must be document _id s from the Story model. Note: ObjectId , Number , String , and Buffer are valid for use as refs.
An embedded, or nested, MongoDB Document is a normal document that's nested inside another document within a MongoDB collection. Embedded documents are particularly useful when a one-to-many relationship exists between documents.
It depends mostly on how you're gonna query and update your data. Consistency and document size is also important in this case. Here's a good summary on when referencing or embedding documents:
Embedding:
Referencing:
This is an exctract from a book on mongo I read. These are just general rules but from my experience, using them makes it very clear wether to reference or embed most of the times.
I would rather reference Forum in this case. But please consider all your requirements. For example if you reference Forum from User and you need to query all User of a particular Forum the query might be slow in this case. If I were you I would compose a list of everything I need and then using general rules would find a balance between pros and cons of embeding and referencing.
Hope it helps!
Personally I like to do references in situations like yours. In this way I can get a comment from a user, a user from a forum, a forum from a comment, a forum from a user, etc. without worrying about doing complicated embedded document queries. I don't even bother storing embedded reference documents. If there is a one to many relationship between a forum and comments then I would store a forum reference on the comment and no comment reference on the forum because when you add/remove comments from the comments collection you then also have to go remove the embedded reference document from the comments collection on the forum.
I can query for a forum from a comment using the forum reference and I can get all comments for a forum by querying the comments collection for that forum reference (which is just an ID number until mongoose populates it behind the scenes for you).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With