I am trying to merge the contents of a configurations collection with a configurations array within a companies collection. So company.configurations is an array, where each element has a reference to an _id in the configurations collection.
> db.configurations.find();
{
"_id" : ObjectId("57518eea52feed9c4f70fc42"),
"name" : "some_config_flag",
"area" : "application",
"default_value" : false
}
{
"_id" : ObjectId("57518f8f52feed9c4f70fc43"),
"area" : "application",
"name" : "some_config_date"
}
> db.companies.find();
{
"_id" : ObjectId("5751935952feed9c4f70fc45"),
"name" : "Demo",
"configurations" : [
{
"configuration_id" : ObjectId("57518eea52feed9c4f70fc42"),
"value" : false
}
]
}
{
"_id" : ObjectId("57519716fe1cc8d0575bcdfc"),
"name" : "Acme",
"configurations" : [
{
"configuration_id" : ObjectId("57518eea52feed9c4f70fc42"),
"value" : true
},
{
"configuration_id" : ObjectId("57518f8f52feed9c4f70fc43"),
"value" : ISODate("2016-06-30T00:00:00Z")
}
]
}
>
I've tried using $lookup to achieve this, but the lookup references remain empty:
db.companies.aggregate([
{
$lookup:{
from:"configurations",
localField:"_id",
foreignField: "configuration_id",
as: "configuration"
}
}])
db.companies.aggregate([
{
$lookup:{
from:"configurations",
localField:"_id",
foreignField: "configurations.configuration_id",
as: "configuration"
}
}])
Also switching the localField & foreignField doesn't help. Is $lookup intended to support this kind of query or would it something to resolve as part of the application code?