2011-10-20 23 views
0

我在这里添加索引:为什么这个mongo索引不起作用?

db.products.ensureIndex({current_features:1}) 

但后来它不会出现在这里的任何影响:

db.products.find({"current_features" : {"$exists" : true}}).explain() 
{ 
    "cursor" : "BasicCursor", 
    "nscanned" : 20995, 
    "nscannedObjects" : 20995, 
    "n" : 2, 
    "millis" : 94, 
    "nYields" : 0, 
    "nChunkSkips" : 0, 
    "isMultiKey" : false, 
    "indexOnly" : false, 
    "indexBounds" : { 

    } 
} 

回答

2

您所要求的具有特定领域,蒙戈使用的每条记录这是自2.0版以来的索引,来自文档:

在v2.0之前,$ exists不能使用索引。其他 字段上的索引仍被使用。

您应该改用sparse index。稀疏索引基本上只索引具有索引字段的文档。所以你的查询将被预先计算,以确保最高速度。