Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-2977

Doc behavior changes related to indexing negations such as $not and $ne

      There have been a few behavior changes in v2.6 related to negations. These should probably go into the 2.6 release notes? See SERVER-13324 and SERVER-12532 or come talk to me for details.

      1) In 2.4 it is possible to get different results for negations depending on whether or not an indexed plan is used:

      > t.drop()
      true
      > t.save({a: 1})
      > t.save({b: 1})
      > t.find({a: {$not: {$gt: 3}}})
      { "_id" : ObjectId("5330803029b74d493d375e46"), "a" : 1 }
      { "_id" : ObjectId("5330803529b74d493d375e47"), "b" : 1 }
      > t.ensureIndex({a: 1})
      > t.find({a: {$not: {$gt: 3}}})
      { "_id" : ObjectId("5330803029b74d493d375e46"), "a" : 1 }
      

      In 2.6, the results will always be the same regardless of whether or not there is an index:

      > t.drop()
      true
      > t.save({a: 1})
      WriteResult({ "nInserted" : 1 })
      > t.save({b: 1})
      WriteResult({ "nInserted" : 1 })
      > t.find({a: {$not: {$gt: 3}}})
      { "_id" : ObjectId("5330808b3a40985f27706d1e"), "a" : 1 }
      { "_id" : ObjectId("5330808d3a40985f27706d1f"), "b" : 1 }
      > t.ensureIndex({a: 1})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > t.find({a: {$not: {$gt: 3}}})
      { "_id" : ObjectId("5330808d3a40985f27706d1f"), "b" : 1 }
      { "_id" : ObjectId("5330808b3a40985f27706d1e"), "a" : 1 }
      

      2) Negation predicates will no longer use sparse indices. This is necessary in order to preserve the invariant from (1), namely that a negation query should return the same results for indexed and unindexed plans. See SERVER-13324.

            Assignee:
            kay.kim@mongodb.com Kay Kim (Inactive)
            Reporter:
            david.storch@mongodb.com David Storch
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              10 years, 4 weeks, 5 days ago