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

Query with empty in clause performs collection scan

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.0.17
    • Component/s: None
    • Labels:
    • Query Optimization
    • ALL
    • Hide

      db.collection.explain().aggregate([{$match: {Field: {$in: []}}}])

      or

      db.collection.find({Field: {$in: []}}).explain("executionStats")

      Where there is no index on Field 

      Show
      db.collection.explain().aggregate([{$match: {Field: {$in: []}}}]) or db.collection.find({Field: {$in: []}}).explain("executionStats") Where there is no index on Field 

      Both find and aggregate will perform a scan when the search criteria is {$in: [ ]} and the searched field is not indexed. 

      IMHO the query should either immediately return zero documents or throw an error due to the empty array.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            peter.williamson@mongodb.com Peter Williamson
            Votes:
            1 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated: