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

Classic $lookup produces incorrect results when localField has 0-prefixed numeric component

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.1.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Query Execution
    • Fully Compatible
    • ALL
    • v7.0, v6.3, v6.0, v5.0, v4.4
    • QO 2023-05-15
    • 105

      Repro:

      b.l.drop();
      db.f.drop();
      assert.commandWorked(db.l.insert([{a: [1]}, {a: {"00": 1}}]));
      assert.commandWorked(db.f.insert({b: 1}));
      
      const pipeline1 = [
          {$lookup: {from: "f", localField: "a.00", foreignField: "b", as: "docs"}}
      ];
      const pipeline2 = [
          {$_internalInhibitOptimization: {}},
          {$lookup: {from: "f", localField: "a.00", foreignField: "b", as: "docs"}}
      ];
      
      // Uses SBE.
      jsTestLog(db.l.aggregate(pipeline1).toArray()); // Only {a: {"00": 1}} matches {b: 1}
      // Uses document source $lookup.
      jsTestLog(db.l.aggregate(pipeline2).toArray()); // Both local documents match {b: 1}
      

      This is likely because SBE treats localField as a regular $match ($match treats "00"-type fields as field names, not indexes).

      Classic $lookup treats "00"-type fields as both an index and a field name. I believe this is unintentional, and likely should be fixed.

            Assignee:
            matt.boros@mongodb.com Matt Boros
            Reporter:
            matt.boros@mongodb.com Matt Boros
            Votes:
            0 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated:
              Resolved: