How to $graphLookup with dynamic parent references in Mongoose

Viewed 10

Let's assume I'm making a react app to manage what's in my restaurant's fridge. I have three collections:

  • Refrigerators (contains jars or food items)
  • Jars (contains jars or food items; exists in refrigerators or other jars)
  • Food Items (exists in jars or fridge)

Refrigerators:

[{
   "name": "abc",
   "temp": 23,
}, ...]

Jars:

[{
   "name": "jar1",
   "parent": 
   {
      "type": "jar",
      "id": "id_of_parent_jar"
   }
}, {
   "name": "jar2",
   "parent": 
   {
      "type": "refrigerator",
      "id": "id_of_parent_refrigerator"
   }
},
 ...]

Food Items:

[{
   "name": "jar1",
   "parent": 
   {
      "type": "jar",
      "id": "id_of_parent_jar"
   }
},
 ...]

A problem arises in any situation where we are using all three collections:

food -> jar -> fridge

There is no way to tell graphLookup to change the from field when the jar parent is a fridge. To further complicate things:

food -> jar -> jar -> fridge

introduces another problem; you cannot assume that depth 1 will be a jar, and depth 2 will be a fridge. Depth 1+n could be any number theoretically until we hit a refrigerator reference.

How would you get graphLookup to dynamically change the from collection based on the parent.type field of either the jar or food_item?

0 Answers
Related