Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-3938

Multiple Joins and a Correlated Subquery with $lookup provided example does not work

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Minor - P4 Minor - P4
    • 4.6
    • Affects Version/s: 4.6
    • Component/s: None
    • None

      Detailed steps to reproduce the problem?

      Example provided in Mongo documentation does not work. 

      Link below:

      https://www.mongodb.com/docs/v7.0/reference/operator/aggregation/lookup/#std-label-lookup-multiple-joins

      Steps to reproduce::

      try:
        db.orders.insert_many( [
          { "_id" : 1, "item" : "almonds", "price" : 12, "ordered" : 2 },
          { "_id" : 2, "item" : "pecans", "price" : 20, "ordered" : 1 },
          { "_id" : 3, "item" : "cookies", "price" : 10, "ordered" : 60 }
        ] )

        db.warehouses.insert_many( [
          { "_id" : 1, "stock_item" : "almonds", "warehouse": "A", "instock" : 120 },
          { "_id" : 2, "stock_item" : "pecans", "warehouse": "A", "instock" : 80 },
          { "_id" : 3, "stock_item" : "almonds", "warehouse": "B", "instock" : 60 },
          { "_id" : 4, "stock_item" : "cookies", "warehouse": "B", "instock" : 40 },
          { "_id" : 5, "stock_item" : "cookies", "warehouse": "A", "instock" : 80 }
        ] )
      except BaseException as e:
        print(e)

      print(list(db.orders.aggregate( [
         {
            "$lookup":
               

      {            "from": "warehouses",            "let": \{ "order_item": "$item", "order_qty": "$ordered" }

      ,
                 "pipeline": [
                    { "$match":
                       { "$expr":
                         

      { "$and":                        [                          \{ "$eq": [ "$stock_item",  "$$order_item" ] }

      ,
                               { "$gte": [ "$instock", "$$order_qty" ] }
                             ]
                          }
                       }
                    },
                    { "$project":

      { "stock_item": 0, "_id": 0 }

      }
                 ],
                 "as": "stockdata"
               }
          }

      ] )))

      As you will see, stockdata field will be empty.

      The exact Python version used, with patch level:

      3.11.4 (main, Jun  7 2023, 10:13:09) [GCC 12.2.0]      

      The exact version of PyMongo used, with patch level:

      4.6.0.dev0

      True

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      Linux b314816f84cf 6.2.0-25-generic #25-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 16

      17:05:07 UTC 2023 x86_64 GNU/Linux  

       

      Web framework or asynchronous network library used, if any, with version (e.g. Django 1.7, mod_wsgi 4.3.0, gevent 1.0.1, Tornado 4.0.2, ...)

            Assignee:
            noah.stapp@mongodb.com Noah Stapp
            Reporter:
            nils@volny.cz Nils Nils
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: