-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.0.1
-
Component/s: Index Maintenance
-
ALL
This was generated from the Google Groups discussion, "sorting, does not use defined index."
http://groups.google.com/group/mongodb-user/browse_thread/thread/9bc121708a0a5cde
To reproduce:
> db.example.drop();
> db.example.save({_id:1, "a":"aaa"});
> db.example.save({_id:2, "a":"bbb"});
> db.example.save({_id:3, "a":"ccc"});
> db.example.save({_id:4, "a":"ddd"});
> db.example.save({_id:5, "a":"eee"});
> db.example.save({_id:6, "a":"fff"});
> db.example.save({_id:7, "a":"ggg"});
> db.example.save({_id:8, "a":"hhh"});
> db.example.ensureIndex(
);
> db.example.find({$or:[
{a:"aaa"},
{a:"bbb"}]}).explain(true)
The index is used, and "nscanned" : 2
Now sort the results:
> db.example.find({$or:[
{a:"aaa"},
{a:"bbb"}]}).sort(
{a:1}).explain(true)
The index is still used, but "nscanned" is now 8, indicating that Mongo is accessing every document in the collection.