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

$lookup matches on paths through arrays differently for local/foreign collections

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • ALL
    • QE 2022-04-04

      db.coll1.find()
      { key: null }
      db.coll2.find()
      { nested: { no_key: 42 } } 
      { nested: [ ] }
      { nested: [ [ ] ] }
      { nested: [ {no_key: 42} ] }
      
      db.coll1.aggregate([{$lookup: {from:"coll2", localField:"key", foreignField:"nested.key", as:"matched"}}, {$project: {_id:0, "matched._id":0}}])
      produces
      { "key" : null, "matched" : [ { "nested" : { "no_key" : 42 } }, { "nested" : [ { "no_key" : 42 } ] } ] }
      That is, docs where the "nested.key" path in foreign traverses through an empty array aren't matched to null.
      
      db.coll2.aggregate([{$lookup: {from:"coll1", localField:"nested.key", foreignField:"key", as:"matched"}}, {$project: {_id:0, "matched._id":0}}])
      produces
      { "nested" : { "no_key" : 42 }, "matched" : [ { "key" : null } ] }
      { "nested" : [ ], "matched" : [ { "key" : null } ] }
      { "nested" : [ [ ] ], "matched" : [ { "key" : null } ] }
      { "nested" : [ { "no_key" : 42 } ], "matched" : [ { "key" : null } ] }
      

      Expected: the relationship of "matching on specified keys" should be symmetric and not depend on which of the collections is local and which is foreign.
      Also see SERVER-63368 that deals with matching empty arrays at the terminal of a path.

      NB: the example above uses empty arrays, but the same behavior applies when the arrays contain non-object values.

            Assignee:
            irina.yatsenko@mongodb.com Irina Yatsenko (Inactive)
            Reporter:
            irina.yatsenko@mongodb.com Irina Yatsenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: