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

Match After $lookup-$unwind not applied correctly to $lookup results

    • Fully Compatible
    • ALL
    • v5.0
    • QO 2021-09-20

      This applies to 5.0.2-rc0

      I have narrowed down that this is not an issue with matching on nested document fields in isolation (they work fine), and also, if the nested results from the unwound lookup result are projected into the root document and the match applied to the root document, it works correctly. Additionally, matches on the fields from the current document of the $lookup (rather than the $lookup result) work correctly. This is the specific situation, minimized, as far as I can tell. Here is a repo query with data (showing results in 4.4 and 5.0.2-rc0)

      Data:
      > db.strings.find()
      { "_id" : ObjectId("611017e028ec416850edf3dc"), "a" : "" }
      { "_id" : ObjectId("611017e328ec416850edf3dd"), "a" : "foo" }
      
      4.4-latest:
      > db.strings.aggregate([{"$lookup": {"from": "strings","let": {},"pipeline": [],"as": "o"}}, {$unwind: "$o"}, {$match: {"o.a": {$ne: ""}}}])
      { "_id" : ObjectId("61101a3728ec416850edf3df"), "a" : "", "o" : { "_id" : ObjectId("61101a3d28ec416850edf3e0"), "a" : "foo" } }
      { "_id" : ObjectId("61101a3d28ec416850edf3e0"), "a" : "foo", "o" : { "_id" : ObjectId("61101a3d28ec416850edf3e0"), "a" : "foo" } }
      
      5.0.2-rc0:
      > db.strings.aggregate([{"$lookup": {"from": "strings","let": {},"pipeline": [],"as": "o"}}, {$unwind: "$o"}, {$match: {"o.a": {$ne: ""}}}])
      { "_id" : ObjectId("611017e028ec416850edf3dc"), "a" : "", "o" : { "_id" : ObjectId("611017e028ec416850edf3dc"), "a" : "" } }
      { "_id" : ObjectId("611017e028ec416850edf3dc"), "a" : "", "o" : { "_id" : ObjectId("611017e328ec416850edf3dd"), "a" : "foo" } }
      { "_id" : ObjectId("611017e328ec416850edf3dd"), "a" : "foo", "o" : { "_id" : ObjectId("611017e028ec416850edf3dc"), "a" : "" } }
      { "_id" : ObjectId("611017e328ec416850edf3dd"), "a" : "foo", "o" : { "_id" : ObjectId("611017e328ec416850edf3dd"), "a" : "foo" } }
      

            Assignee:
            katherine.wu@mongodb.com Katherine Wu (Inactive)
            Reporter:
            patrick.meredith@mongodb.com Patrick Meredith
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: