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

Accessing nested array variable in $lookup+$let+$expr seems undefined

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.7, 6.0.3
    • Component/s: None
    • Labels:
      None
    • Query Execution
    • ALL
    • Hide

      Create collection "machine"  with data

      {"_id":"qqq","lines":[{"devices":{"type-c":{"disabled":false}}}]} 

      Create collection device with data

      {"machineId":"qqq","deviceEnabled":false}
      {"machineId":"qqq","deviceEnabled":false} 

      Execute aggregation

      db.machine.aggregate([    {
              $lookup: {
                  'from': 'device',
                  let: {mId: '$_id', m_device_disabled: "$lines.devices.type-c.disabled"},
                  as: 'res',
                  pipeline: [
                      {
                          $match:
                          {
                              $expr:
                              {
                                  $and:
                                      [
                                          { $eq: ['$$mId', '$machineId'] }
                                         ,{ $eq: [{$toBool: '$$m_device_disabled'},{$toBool: "$deviceEnabled"}] }
                                      ]
                              }
                          }
                      }]
              }
          }
          ,   {
               $project: {
                 'res.deviceEnabled': 1, 
                 'res._id': 1
                 ,'_id': 1
               }
           }
          ])
          
          

      Expected result:

      {
          "_id" : "qqq",
          "res" : [
              {
                  "_id" : ObjectId("6371fcc7ce3e296eed4c2b0c"),
                  "deviceEnabled" : false
              },
              {
                  "_id" : ObjectId("6371fcc7ce3e296eed4c2b32"),
                  "deviceEnabled" : false
              }
          ]
      } 

      Actual result:

      {
          "_id" : "qqq",
          "res" : [ ]
      } 
      Show
      Create collection "machine"  with data { "_id" : "qqq" , "lines" :[{ "devices" :{ "type-c" :{ "disabled" : false }}}]} Create collection device with data { "machineId" : "qqq" , "deviceEnabled" : false } { "machineId" : "qqq" , "deviceEnabled" : false } Execute aggregation db.machine.aggregate([    {         $lookup: {             'from' : 'device' ,             let: {mId: '$_id' , m_device_disabled: "$lines.devices.type-c.disabled" },             as: 'res' ,             pipeline: [                 {                     $match:                     {                         $expr:                         {                             $and:                                 [                                     { $eq: [ '$$mId' , '$machineId' ] }                                    ,{ $eq: [{$toBool: '$$m_device_disabled' },{$toBool: "$deviceEnabled" }] }                                 ]                         }                     }                 }]         }     }     ,   {          $project: {             'res.deviceEnabled' : 1,              'res._id' : 1            , '_id' : 1          }      }     ])          Expected result: {     "_id" : "qqq" ,     "res" : [         {             "_id" : ObjectId( "6371fcc7ce3e296eed4c2b0c" ),             "deviceEnabled" : false         },         {             "_id" : ObjectId( "6371fcc7ce3e296eed4c2b32" ),             "deviceEnabled" : false         }     ] } Actual result: {     "_id" : "qqq" ,     "res" : [ ] }

      I'm trying to make a join of two colections by multiple fields, one of them is boolean and in case when this field is "false"

        1. repro.js
          2 kB
        2. test.device.json
          0.1 kB
        3. test.machine.json
          0.1 kB

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            igornem1@gmail.com Igor Nem
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: