I have these collections:
Recordings: { _id, call }
Calls: { _id, department }
Departments: { _id, settings }
Settings: { _id, …settingsData }
I need to retrieve many recordings and attach the corresponding “setting” to each one. Considering that the collections “Recordings” and “Calls” are extensive, and the matching recordings (starting point) will also be a very large amount of documents, what’s the best approach?
Nest several lookups (at least 3 to get from recordings to settings) but having everything in one query
Request multiple queries to DB, one to populate the department, and another to get the specific setting per department (considering this will require merging the information in execution time afterward)
In my opinion, embedding the Department and Setting objects in the Recordings and Calls documents can help avoid joins in your main query.
However, you must handle cases where the Department or Setting updates you must execute queries to update the embedded objects in the Recordings and Calls collections.
If the total number of documents in the Department and Settings is less or the size is less then you can fetch them all together through a separate query and merge it with the respective Recordings and Calls. This is better than the first option (nested lookup) if you are fetching large amounts of documents from Recordings and Calls.
I actually only need to do this once or maybe twice, I think I’ll take recommendation from @turivishal since Calls is a very large collection but Departments and Settings are not, and most of them are repeated across Calls. Thank you both!