在我的MongoDB查询结果中删除"scanAndOrder":true

Art*_*ves 5 sorting indexing sharding mongodb

所以我在我的数据库中有一个带有以下shardkey的集合:{cl:"yyyy-mm",user_id:N}

当我执行以下查询

db.collection.find({ cl:"2012-03", user_id:{$in:users}, _id:{"$lt":new ObjectId('4f788b54204cfa4946000044')} }).sort({_id:-1}).limit(5).explain(true)
Run Code Online (Sandbox Code Playgroud)

它给了我这个结果:

"clusteredType" : "ParallelSort",
"shards" : {
    "set1/hostname.com:27018" : [
        {
            "cursor" : "BtreeCursor cl_1_user_id_1 multi",
            "nscanned" : 21294,
            "nscannedObjects" : 21288,
            "n" : 5,
            "scanAndOrder" : true,
            "millis" : 1258,
            "nYields" : 69,
            "nChunkSkips" : 0,
            "isMultiKey" : false,
            "indexOnly" : false,
            "indexBounds" : { ...
Run Code Online (Sandbox Code Playgroud)

那么如何才能使用索引进行排序然后我不需要扫描所有21288文档,只返回最后5个?

jmc*_*mcd 2

尝试 Dex 以确保索引按照您认为应该的方式工作:https ://github.com/mongolab/dex