I am trying to break the RDBMS mentality with Mongoose in a node.js application and may use some recommendations regarding best practices.
A simple example: let's say there are two collections, organizations and contacts. This is a one-to-many relationship, and they should both be single. However, in the index method, I want to return an array of organization objects, each of which has an array of member contact objects. (And later, I want to return an array of contacts, each of which has an organization object.)
It seems to me that I do not understand how best to do this. Here are the options that I see so far:
Built-in contacts with their identifiers in organizational documents. Then the index query may be Organization.find({}, function(err, orgs) { orgs[n].contacts[n]...}).
Disadvantage : a large amount of additional storage is required and it is necessary to update the built-in contact documents at any time when you change the contact document "master".
Use the Mongoose populate / DBRef structure to store foreign key identifiers in both tables. The request may be as follows: Organization.find({}}.populate('contacts').run(...).
Disadvantage : you need to store keys on both sides.
Go to the more traditional "foreign key" route, saving only the organization identifier in the contact document. Request organizational documents, and then request all contacts in the organization request callback, combining the data into a single object to return.
Disadvantage : multiple requests and additional processing overhead (I believe that we must independently find organizations and contacts for these organizers, and then repeat them each, manually matching in the common _id field, to combine them into a new output object).
I must miss the best option. Something that allows you to use the scheme number 3, but returns the desired combined object with much lower cost.
. async , , / ?