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

$lookup with array local field misbehaves in pipeline

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.4
    • Component/s: Aggregation Framework
    • None
    • ALL
    • Hide
      db.classes.insert([
        {
          _id: 1.0,
          title: "Reading is ...",
          enrollmentlist: ["giraffe2", "pandabear", "artie"],
          days: ["M", "W", "F"]
        },
        {
          _id: 2.0,
          title: "But Writing ...",
          enrollmentlist: ["giraffe1", "artie"],
          days: ["T", "F"]
        },
        {
          _id: 3.0,
          title: "Math",
          enrollmentlist: ["giraffe1", "artie"],
          days: ["T", "F"]
        },
        {
          _id: 4.0,
          title: "Science",
          enrollmentlist: ["giraffe1", "artie"],
          days: ["T", "F"]
        }
      ]);
      

       

       
      db.members.insert([
        {
          _id: 1.0,
          name: "artie",
          names: ["giraffe2", "pandabear", "artie"],
          joined: ISODate("2016-05-01T00:00:00.000Z"),
          status: "A"
        },
        {
          _id: 2.0,
          name: "giraffe",
          names: ["giraffe", "yyy"],
          joined: ISODate("2017-05-01T00:00:00.000Z"),
          status: "D"
        },
        {
          _id: 3.0,
          name: "giraffe1",
          names: ["giraffe1", 33],
          joined: ISODate("2017-10-01T00:00:00.000Z"),
          status: "A"
        },
        {
          _id: 4.0,
          name: "panda",
          names: ["panda", "r"],
          joined: ISODate("2018-10-11T00:00:00.000Z"),
          status: "A"
        },
        {
          _id: 5.0,
          name: "pandabear",
          names: ["pandabear", 123],
          joined: ISODate("2018-12-01T00:00:00.000Z"),
          status: "A"
        },
        {
          _id: 6.0,
          name: "giraffe2",
          names: ["giraffe2", "www"],
          joined: ISODate("2018-12-01T00:00:00.000Z"),
          status: "D"
        }
      ]);
      

      Then observe that

       
      db.members.aggregate([
        {
          $lookup: {
            from: "classes",
            let: { names: "$names" }, // ----> $names is an array
            pipeline: [{ $match: { $expr: { $in: ["$$names", "$enrollmentlist"] } } }],
            as: "classes_info"
          }
        }
      ]);
      

       
      returns empty join results, while

       
      db.members.aggregate([
        {
          $lookup: {
            from: "classes",
            let: { names: "$name" },  // ----> $name is a string
            pipeline: [{ $match: { $expr: { $in: ["$$names", "$enrollmentlist"] } } }],
            as: "classes_info"
          }
        }
      ]);
      

       
      does not. It seems arrays are not represented correctly in the pipeline stage of a $lookup
       
      I'm on version 4.0.4 - apologies if this was fixed in a later version - I can't find any references to it, if so.

      Show
      db.classes.insert([   {     _id: 1.0,     title: "Reading is ...",     enrollmentlist: ["giraffe2", "pandabear", "artie"],     days: ["M", "W", "F"]   },   {     _id: 2.0,     title: "But Writing ...",     enrollmentlist: ["giraffe1", "artie"],     days: ["T", "F"]   },   {     _id: 3.0,     title: "Math",     enrollmentlist: ["giraffe1", "artie"],     days: ["T", "F"]   },   {     _id: 4.0,     title: "Science",     enrollmentlist: ["giraffe1", "artie"],     days: ["T", "F"]   } ]);     db.members.insert([   {     _id: 1.0,     name: "artie",     names: ["giraffe2", "pandabear", "artie"],     joined: ISODate("2016-05-01T00:00:00.000Z"),     status: "A"   },   {     _id: 2.0,     name: "giraffe",     names: ["giraffe", "yyy"],     joined: ISODate("2017-05-01T00:00:00.000Z"),     status: "D"   },   {     _id: 3.0,     name: "giraffe1",     names: ["giraffe1", 33],     joined: ISODate("2017-10-01T00:00:00.000Z"),     status: "A"   },   {     _id: 4.0,     name: "panda",     names: ["panda", "r"],     joined: ISODate("2018-10-11T00:00:00.000Z"),     status: "A"   },   {     _id: 5.0,     name: "pandabear",     names: ["pandabear", 123],     joined: ISODate("2018-12-01T00:00:00.000Z"),     status: "A"   },   {     _id: 6.0,     name: "giraffe2",     names: ["giraffe2", "www"],     joined: ISODate("2018-12-01T00:00:00.000Z"),     status: "D"   } ]); Then observe that   db.members.aggregate([   {     $lookup: {       from: "classes",       let: { names: "$names" }, // ----> $names is an array       pipeline: [{ $match: { $expr: { $in: ["$$names", "$enrollmentlist"] } } }],       as: "classes_info"     }   } ]);   returns empty join results, while   db.members.aggregate([   {     $lookup: {       from: "classes",       let: { names: "$name" },  // ----> $name is a string       pipeline: [{ $match: { $expr: { $in: ["$$names", "$enrollmentlist"] } } }],       as: "classes_info"     }   } ]);   does not. It seems arrays are not represented correctly in the pipeline stage of a $lookup   I'm on version 4.0.4 - apologies if this was fixed in a later version - I can't find any references to it, if so.
    • Query 2019-11-04

          Assignee:
          david.storch@mongodb.com David Storch
          Reporter:
          adamrackis@hotmail.com Adam Rackis
          Votes:
          0 Vote for this issue
          Watchers:
          6 Start watching this issue

            Created:
            Updated:
            Resolved: