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

make sparse indexes error out on {$exists: false} queries

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.1.2
    • Affects Version/s: 2.0.0
    • Component/s: Querying
    • None
    • Major Change
    • ALL

      When querying for documents in which a field does not exist, the optimiser will consider using a sparse index on that field, but this means that it will never find anything.

      > db.test.insert({});
      > db.test.insert({});
      > db.test.insert({});
      > db.test.insert({});
      > db.test.insert({});
      > db.test.find({foo: {$exists: false}}).count();
      5
      > db.test.ensureIndex(

      {foo: 1}

      ,

      {sparse: true, safe: true}

      );
      > db.test.find({foo: {$exists: false}}).count();
      0
      > db.test.find({foo: {$exists: false}}).explain();
      {
      "cursor" : "BtreeCursor foo_1",
      "nscanned" : 0,
      "nscannedObjects" : 0,
      "n" : 0,
      "millis" : 0,
      "nYields" : 0,
      "nChunkSkips" : 0,
      "isMultiKey" : false,
      "indexOnly" : false,
      "indexBounds" :

      { "foo" : [ [ null, null ] ] }

      }

            Votes:
            12 Vote for this issue
            Watchers:
            15 Start watching this issue

              Created:
              Updated:
              Resolved: