2013-10-08 64 views

回答

2

是的,MongoDB可以使用复合索引对索引键进行反向排序。

如果你有一个复合索引:

db.test.ensureIndex({a: -1, b: -1, c: -1}) 

explain结果表明,BTreeCursor使用

db.test.find().sort({a: 1, b:1, c:1}).explain() 

     "cursor" : "BtreeCursor a_-1_b_-1_c_-1 reverse", 
     "isMultiKey" : false, 
     "n" : 11, 
     "nscannedObjects" : 11, 
     "nscanned" : 11, 
     "nscannedObjectsAllPlans" : 11, 
... 

复合索引将被使用,如果你颠倒所有的排序顺序的索引前缀。

但是,db.test.find().sort({a:1, b: 1, c: -1})将不能使用索引,因此将使用BasicCursor

相关问题