allow $expr with $in expression to use indexes

XMLWordPrintableJSON

    • Query Optimization
    • Fully Compatible
    • v8.0, v7.0, v6.0
    • 0
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Similarly to SERVER-31760 it's possible to rewrite $expr with $in expression to an indexable MatchExpression.

      Semantically

       {$expr:{$in:[ "$value", [ "v1", "v2", "v3" ]] } } 

      is exactly equivalent to

       {"value":{$in:[ "v1", "v2", "v3"]}} 

      if value is not an array.

      If we restrict using an index for $expr $in expressions to non-multikey indexes/paths, then we will only get back matches from the index that are guaranteed to be correct semantically to the aggregation meaning of $in.

      This is also exactly equivalent query that can use any index and return only agg semantics:

      db.reviews.find({product_id:{$in:["hat","shirt"],$not:{$elemMatch:{$exists:true}}}})
      

      The second part prevents reaching into arrays for comparison.

              Assignee:
              Lynne Wang
              Reporter:
              Asya Kamsky
              Votes:
              28 Vote for this issue
              Watchers:
              52 Start watching this issue

                Created:
                Updated:
                Resolved: