I'm trying to break down my RDBMS mentality with Mongoose in a node.js app, and could use some guidance as to best practices.
A simple example: Let's say there are two collections, organizations and contacts. It's a one-to-many relationship and they both need to be lonely. In an index method, however, I want to return an array of organization objects, each with an array of member contact objects. (And later I want to return an array of contacts, each with an organization object.)
It seems to me that I'm misunderstanding the best way to accomplish this. Here are the options as I see them thus far:
Embed contacts with their ids in the organization documents. The index query could then be Organization.find({}, function(err, orgs) { orgs[n].contacts[n]...})
.
Disadvantage: Lots of extra storage necessary, and have to update embedded contact documents any time you change the "master" contact document.
Take advantage of Mongoose's populate/DBRef structure to store "foreign key" ids in both tables. The query can then be: Organization.find({}}.populate('contacts').run(...)
.
Disadvantage: You have to store keys on both sides.
Go the more traditional "foreign key" route, storing just the organization id in a contact document. Query the organization documents and then query all the contacts in the organization query's callback, combining the data into one object to return. Disadvantage: Multiple queries and extra processing overhead (I believe we have to find the orgs and contacts for those orgs independently and then iterate back over each, matching manually on the common _id field, to combine them into a new output object).
I must be missing a better option. Something that allows for the schema of #3 but returns the desired combined object with much less overhead.
NOTE: The async module seems like it could help with this, but is this really the only/best way?
This really depends on the queries you are going to make. - Does your site depend more on contacts or on organizations? - Will you often need to display all contacts from an organization? - What details for contacts do you need to display on an organization page?
Etc.
So this really depends on your project's needs. Are you organizations and contacts page constantly being updated? If not than duplicating data doesn't seem such a bad idea, especially if your site is more read heavy.
Bottom line: think on what pages you'll have on the site and what will that pages need to display (thus what queries would need to be made).
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