How to perform properly $lookup aggregation query deep into field in mongodb?
templates
{
"_id": ObjectID("589888769ffec515d86f3e6c"),
"tpl_name": "t1",
"tpl_user": "some"
},
{
"_id": ObjectID("589888769ffec123386f3222"),
"tpl_name": "t2",
"tpl_user": "some"
}
subscriptions
{
"_id": ObjectID("58c182ed320386f56a518efb"),
"data": {
"template": "589888769ffec515d86f3e6c",
"email": "some.email@gmail.com"
}
},
{
"_id": ObjectID("58c182ed320386f56a518e7f"),
"data": {
"template": "589888769ffec515d86f3e6c",
"email": "some.email@gmail.com"
}
}
Need to get list of templates with subscriptions in it.
I tried to get it like that.
db.collection('templates').aggregate([
{$match: { 'tpl_user': tpl_user.id }},
{
$lookup: {
from: "subscriptions",
localField: "_id",
// tried to use foreignField like that
foreignField: "data.template",
as: "subscriptions"
}
},
])
But no dice. Query returns empty subscriptions array.