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

Querying against sparse index creates bad state, affecting later queries

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.0.3, 2.1.0
    • Affects Version/s: 2.0.1
    • Component/s: Index Maintenance, Querying
    • None
    • Environment:
      Shell and DB running under OSX.
    • OS X

      I have run into a case where querying against a sparse index on a nested field puts the DB in a bad state that affects later queries on the same collection. I originally discovered this problem using the ruby driver, but was able to reproduce the problem completely from the mongo console (see below). I inserted comments, prefixed with #. The mongo_bug db did not exist prior to this example.

      > use mongo_bug
      switched to db mongo_bug
      > db.bug_collection.ensureIndex(

      {"nested.key2": 1}

      ,

      {sparse: true}

      )
      > db.bug_collection.insert(

      {key: 'a'}

      )
      > db.bug_collection.insert(

      {key: 'b'}

      )
      > db.bug_collection.insert(

      {key: 'c'}

      )
      > db.bug_collection.find({})

      { "_id" : ObjectId("4f0e7daf9be3426d0ba2e7b8"), "key" : "a" } { "_id" : ObjectId("4f0e7db59be3426d0ba2e7b9"), "key" : "b" } { "_id" : ObjectId("4f0e7dba9be3426d0ba2e7ba"), "key" : "c" }

      > db.bug_collection.find({key: {$in: ['a', 'b', 'c']}, "nested.key2": {$ne: 'd'}})

      1. No results returned (expected, because of the sparse index?)
        > db.bug_collection.find({key: {$in: ['a', 'b', 'c']}})
      2. No results returned (NOT expected)
        > db.bug_collection.dropIndex("nested.key2_1") { "nIndexesWas" : 2, "ok" : 1 }

        > db.bug_collection.find({key: {$in: ['a', 'b', 'c']}})

        { "_id" : ObjectId("4f0e7daf9be3426d0ba2e7b8"), "key" : "a" } { "_id" : ObjectId("4f0e7db59be3426d0ba2e7b9"), "key" : "b" } { "_id" : ObjectId("4f0e7dba9be3426d0ba2e7ba"), "key" : "c" }
      3. Query now works, after the index has been dropped.

      The order of the queries also matters:

      > db.bug_collection.dropIndex("nested.key2_1")

      { "nIndexesWas" : 2, "ok" : 1 }

      > db.bug_collection.ensureIndex(

      {"nested.key2": 1}

      ,

      {sparse: true}

      )
      > db.bug_collection.find({key: {$in: ['a', 'b', 'c']}})

      { "_id" : ObjectId("4f0e811f89fc95d0a1a0356b"), "key" : "a" } { "_id" : ObjectId("4f0e812389fc95d0a1a0356c"), "key" : "b" } { "_id" : ObjectId("4f0e812589fc95d0a1a0356d"), "key" : "c" }
      1. Results returned (expected)
        > db.bug_collection.find({key: {$in: ['a', 'b', 'c']}, "nested.key2": {$ne: 'd'}}) { "_id" : ObjectId("4f0e811f89fc95d0a1a0356b"), "key" : "a" } { "_id" : ObjectId("4f0e812389fc95d0a1a0356c"), "key" : "b" } { "_id" : ObjectId("4f0e812589fc95d0a1a0356d"), "key" : "c" }
      2. Results returned (this same query returns nothing in first example)
        > db.bug_collection.dropIndex("nested.key2_1") { "nIndexesWas" : 2, "ok" : 1 }

        > db.bug_collection.ensureIndex(

        {"nested.key2": 1}

        ,

        {sparse: true}

        )
        > db.bug_collection.find({key: {$in: ['a', 'b', 'c']}, "nested.key2": {$ne: 'd'}})

      3. No results returned (as in first example)
        > db.bug_collection.find({key: {$in: ['a', 'b', 'c']}})
      4. No results returned (as in first example)

      It's as if the queries are considered equivalent and being cached.

            Assignee:
            aaron Aaron Staple
            Reporter:
            canavese Paul Canavese
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: