DEV Community

Discussion on: MongoDB lookup on an array of objects which contains (foreign) ObjectId as a key

Collapse
 
phatlaunchdeck profile image
Hoang-Phat Pham

Man I spent countless days searching for a solution on my own project which has the exact model schema (with slightly different fields names), thank you so much!

One thing I would like to ask if you don't mind: in my schema I actually have 2 arrays that have the same structure with your "products". If I apply the above method, only the latter array would be populated, I guess that is because of the "re-fetch root document step" would override the first array that gets populated.

Any idea on this?

Collapse
 
krishnabose02 profile image
Krishna Bose

Hey, thats a good question. At this point I cannot find any good solution for the scenario, but one thing I've learnt lately is that if your resource is read intensive, you should consider having a structure of data that allows you to fetch the documents without multiple lookups. Even if this means duplication of data, as long as you have high read to write ratio (say 100:1) or even higher, then it still results in a optimal strategy.