-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
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(
,
{sparse: true})
> db.bug_collection.insert(
)
> db.bug_collection.insert(
)
> db.bug_collection.insert(
)
> db.bug_collection.find({})
> db.bug_collection.find({key: {$in: ['a', 'b', 'c']}, "nested.key2": {$ne: 'd'}})
- No results returned (expected, because of the sparse index?)
> db.bug_collection.find({key: {$in: ['a', 'b', 'c']}}) - 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" } - 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']}})
- 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" } - 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'}}) - No results returned (as in first example)
> db.bug_collection.find({key: {$in: ['a', 'b', 'c']}}) - No results returned (as in first example)
It's as if the queries are considered equivalent and being cached.