Need to do a lookup and I have an object(s) within an Object. I have spent quite an amount of time looking for examples similar to this, but am only finding example where there is an array of objects within an object…
I have seemed to simplify this down to not even knowing how to how to access objects with an Object.
- _id – ObjectID Always present
- email – String Always present
- dailyIntake – Object Always present
- breakfast – Object, Not required / not always present
- Ingredient – Array
- lunch – Object, Not required / not always present
- Ingredient – Array
- dinner – Object, Not required / not always present
- Ingredient – Array
- breakfast – Object, Not required / not always present
Shown below:
Daily Intake – Collection
[
{
"name": "Taylor Scott",
"email": "taylor_scott@fakegmail.com",
"dailyIntake": {
"breakfast": {
"foodItem": [
"pancakes"
]
},
"lunch": {
"foodItem": [
"sandwhich"
]
},
"dinner": {
"foodItem": [
"mushroom soup",
"salad"
]
}
}
}
]
NOTE: Not required that breakfast, lunch, or dinner are present
Now I have another collection:
Food – Collection
[
{
"_id": "egg",
"consumer_name": "Taylor Scott",
"brand": "FarmFresh",
"distributor": "Whole Foods"
}
]
I would like to do a $lookup that gives me something like this:
[
{
"name": "Taylor Scott",
"email": "taylor_scott@fakegmail.com",
"dailyIntake": {
"breakfast": {
"foodItem": [
"pancakes"
]
},
"lunch": {
"foodItem": [
"sandwhich"
]
},
"dinner": {
"foodItem": [
"mushroom soup",
"salad"
]
}
},
"dailyIntakeObjects": [
{
"_id": "egg",
"consumer_name": "Taylor Scott",
"brand": "FarmFresh",
"distributor": "Whole Foods"},
{ <ham object> },
{ <mushroom soup object>},
{ <salad object>}
}
]
}
]