Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-28717

$lookup does not expand 'localField' as expected when it is a dotted path representing fields within an array

    • Fully Compatible
    • ALL
    • v3.4
    • Query 2017-05-29

      db.a.find()
      { "_id" : ObjectId("58ebbf82016ce019673b111c"), "a" : [ { "_id" : 1 }, { "_id" : 2 } ] }
      { "_id" : ObjectId("58ebbf8d016ce019673b111d"), "a" : [ { "_id" : 5 }, { "_id" : 12 } ] }
      > db.b.find()
      { "_id" : 1 }
      { "_id" : 2 }
      { "_id" : 3 }
      { "_id" : 5 }
      { "_id" : 12 }
      

      I would expect $lookup using "a._id" as localField to work since SERVER-22881 was fixed, however:

      db.a.aggregate({$lookup:{from:"b", localField:"a._id", foreignField:"_id", as:"details"}})
      { "_id" : ObjectId("58ebbf82016ce019673b111c"), "a" : [ { "_id" : 1 }, { "_id" : 2 } ], "details" : [ ] }
      { "_id" : ObjectId("58ebbf8d016ce019673b111d"), "a" : [ { "_id" : 5 }, { "_id" : 12 } ], "details" : [ ] }
      

      "details" is properly populated only if I unwind "a" and use "a._id" or if I create another field first in $addFields or $project from "$a._id" (which makes it a simple array) then it works as localField:

      db.a.aggregate({$addFields:{a:"$a._id"}},{$lookup:{from:"b", localField:"a", foreignField:"_id", as:"details"}})
      { "_id" : ObjectId("58ebbf82016ce019673b111c"), "a" : [ 1, 2 ], "details" : [ { "_id" : 1 }, { "_id" : 2 } ] }
      { "_id" : ObjectId("58ebbf8d016ce019673b111d"), "a" : [ 5, 12 ], "details" : [ { "_id" : 5 }, { "_id" : 12 } ] }
      

            Assignee:
            james.wahlin@mongodb.com James Wahlin
            Reporter:
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: