0

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?

Kris Lamote
  • 372
  • 2
  • 8
  • FYI: trying to make this focused to $lookup.I am aware that if $lookup cannot be used, that I should probably change the schema design better serving a solution on the application code side – Kris Lamote Jun 03 '16 at 15:44
  • 1
    In MongoDB 3.2 It will not work. You need to upgrade to MongoDB 3.3.4 or newer version to make it work – styvane Jun 03 '16 at 16:38
  • @user3100115 thanks, the unwind/group solution from [$lookup on ObjectId's in an array](http://stackoverflow.com/questions/34967482/lookup-on-objectids-in-an-array) indeed works if its an array of ObjectIds. Upgrading to 3.3.4 with homebrew is apparently not an option. I go with some schema adjustments and tackling the missing portions in the application – Kris Lamote Jun 03 '16 at 20:01

0 Answers0