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

$lookup matches empty arrays to null/missing/undefined

    • 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-02-21, QE 2022-03-21

      Local collection L: [{lkey:[]}]
      Foreign collection F: [{fkey: null},{fkey:undefined},{no_fkey:true}]
      Query: db.L.aggregate({$lookup: {from: "F", localField: "lkey", foreignField: "fkey", as: "matched"}}

      The query matches all three foreign records to the local record.
      According to the comment in
      https://github.com/10gen/mongo/blob/f65c3ec8d02162327c80f11fbb1758096a75cbe8/src/mongo/db/pipeline/document_source_lookup.cpp#L839-L842 the behavior doesn't seem to be intentional.

      Proposed semantics for matching empty arrays ("matches X" below means "matches value X from the foreign field").

      If the local key is "a":
      {a: []} -> no matches
      {a: [[], 1]} -> matches [] and 1
      {a: [[[]]]} -> matches [[]] (does not match [])
      {b: []} -> matches null
      {b: [[]]} -> matches null
      
      If the local key is "a.x":
      {a: {x: []}} -> no matches
      {a: {x: 1}} -> matches 1
      {a: {x: [1, 2]}} -> matches 1 and 2
      {a: [{x: 1}, {x: 2}]}} -> matches 1 and 2
      {a: {x: [[]]}} -> matches []
      {a: {y: 42}} -> matches null
      {a: {y: []}} -> matches null
      {a: [{y:[]}]} -> matches null
      {a: [{y:42}, {x:[]}]} -> matches null
      {a: [{y:42}, {x:[[]]}]} -> matches null and []
      
      If the local key is "a.x.w":
      {a: [{x: {w: []}}]} -> no matches
      {a: [{x:[]}, {x: {w: []}}]} -> matches null
      {a: [{x:[]}, {x: {w: 1}}]} -> matches null and 1
      {a: [{x:[]}, {x: {w: [[]]}}]} -> matches null and []
      

            Assignee:
            yoonsoo.kim@mongodb.com Yoon Soo Kim (Inactive)
            Reporter:
            irina.yatsenko@mongodb.com Irina Yatsenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            17 Start watching this issue

              Created:
              Updated:
              Resolved: