5

I am also using Node - which I am not the most familiar with - if the articles/books/tutorials pertained to this also - that would help even more.

I come from a mostly MySQL background - but in this api, all data is going to be served as JSON - so at first glance, using Mongo made a lot of sense (I could be wrong). One thign I have been unable to wrap my head around though, is relational models. e.b. Object has many Assets.

To use some examples most people would be familiar with, let's assume I am trying to do something like:

  • Select a blog post and all of its comments. or
  • Select a photo gallery and all of its photos

Is it as simple as storing a call back inside of my assets like this?:

Photo {
    __id: 5,
    content: '...',
    gallery_id: 9 
}

And performing a .find({ gallery_id: 9 })?

I know this would work, but is that best practice? I am very new to Document Databases, and have not seen much on this particular topic.

4
  • 1
    The close reasons on this post are not right; OP is not asking if he has to use MongoDB, he's asking how he should organize his data in MongoDB, which is a fairly common wondering across programmers. – Florian Margaine Aug 27 '13 at 17:37
  • @FlorianMargaine Thank you for your comment. The question does not appear closed to me. Did it get opened back up since your comment? – drewwyatt Aug 27 '13 at 18:17
  • 1
    There are 3 close votes that were casted. It needs 5 to be closed, but I thought commenting before the question gets closed was better than waiting for it to be closed. – Florian Margaine Aug 27 '13 at 18:41
  • @FlorianMargaine I really appreciate it! – drewwyatt Aug 27 '13 at 22:32
4

Coincidentally, I am in the process of thinking through a new application using Node and MongoDB.

I have lightly used MongoDB in the past and found the MongoDB documentation data modeling to be a great starting point. The approach that you describe, storing a reference in the parent and using a second find to get the referenced items, is a perfectly good way to go about it, if you choose to make use of references rather than directly embedding the children in the parent object.

However, there might be some better ways to go at this. I have been looking into using Mongoose as an Object Document Mapper (ODM). It is the officially supported ODM for Node per the MongoDB documentation.

It provides a number of useful features, particularly if you are building a fairly complex model within the application, including pseudo-joins using the populate function. The population capability provides a more structured approach to managing references allowing for retrieval of parent and children in one step.

0
6

MongoDB models relationships by using references.

To normalize data, store references between two documents to indicate a relationship between the data represented in each document.

In general, use normalized data models:

  • when embedding would result in duplication of data but would not provide sufficient read performance advantages to outweigh the implications of the duplication.
  • to represent more complex many-to-many relationships.
  • to model large hierarchical data sets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.