Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

javascript - Embedded document vs reference in mongoose design model?

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • Small subdocuments
  • Data that does not change regularly
  • Eventual consistency is acceptable
  • Document that grow by a small amout
  • Data that you will often need to perform a second query to fetch
  • Fast reads

Referencing:

  • Large subdocuments
  • Volatile data
  • Immediate consistency is necessary
  • Document that grow a large amount
  • Data that you will often exclude from results
  • Fast writes

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...