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

$expr does not use index for $in

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 3.6.7, 4.0.2
    • Fix Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      Create a new collection.

      Create index :

      db.debug.createIndex({ 'item': 1 })

      Insert data :

      db.debug.insert({ item: "card", qty: 15 })
      db.debug.insert({ item: "table", qty: 15 })
      db.debug.insert({ item: "piano", qty: 15 })
      db.debug.insert({ item: "house", qty: 15 })
      db.debug.insert({ item: "test", qty: 15 })
      

      Query :

      db.debug.explain().aggregate([{
      "$match": {"$expr": {"$in": ["$item", ["card", "test"]]}}
      }])

      Wining Plan

      "winningPlan" : {
          "stage" : "COLLSCAN", 
          "filter" : {
              "$expr" : {
                  "$in" : [
                      "$item", 
                      {
                          "$const" : [
                              "card", 
                              "test"
                          ]
                      }
                  ]
              }
          }, 
          "direction" : "forward"
      },
      

       

      Show
      Create a new collection. Create index : db.debug.createIndex({ 'item' : 1 }) Insert data : db.debug.insert({ item: "card" , qty: 15 }) db.debug.insert({ item: "table" , qty: 15 }) db.debug.insert({ item: "piano" , qty: 15 }) db.debug.insert({ item: "house" , qty: 15 }) db.debug.insert({ item: "test" , qty: 15 }) Query : db.debug.explain().aggregate([{ "$match" : { "$expr" : { "$in" : [ "$item" , [ "card" , "test" ]]}} }]) Wining Plan "winningPlan" : { "stage" : "COLLSCAN" , "filter" : { "$expr" : { "$in" : [ "$item" , { "$const" : [ "card" , "test" ] } ] } }, "direction" : "forward" },  

      Description

      An $expr expression in a query will not use an index when used with $in.

      The goal was to used it in a $lookup pipeline.

      The problem seems to exists in 3.6.3 as well as 4.0.2

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              Mathias Claux Mathias C.
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: