mongodb – 为什么indexOnly == false

我有一个索引集合:

{
    "UserId" : 1,
    "ShareId" : 1,
    "ParentId" : 1,
    "DeletedDate" : 1
}

如果我查询:

db.Files.find({ "UserId" : ObjectId("5450d837f32a1e098c844e2a"),
    "ShareId" : ObjectId("5450d879f32a1e098c844e94"),
    "ParentId" : ObjectId("5450d8af6a092a0b74a44026"),
    "DeletedDate":null},
    {_id:0, ShareId:1}).explain()   

输出说“indexOnly”:false:

{
"cursor" : "BtreeCursor UserId_1_ShareId_1_ParentId_1_DeletedDate_1",
"isMultiKey" : false,
"n" : 2120,
"nscannedObjects" : 2120,
"nscanned" : 2120,
"nscannedObjectsAllPlans" : 2318,
"nscannedAllPlans" : 2320,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 21,
"nChunkSkips" : 0,
"millis" : 42,
"indexBounds" : {
    "UserId" : [ 
        [ 
            ObjectId("5450d837f32a1e098c844e2a"), 
            ObjectId("5450d837f32a1e098c844e2a")
        ]
    ],
    "ShareId" : [ 
        [ 
            ObjectId("5450d879f32a1e098c844e94"), 
            ObjectId("5450d879f32a1e098c844e94")
        ]
    ],
    "ParentId" : [ 
        [ 
            ObjectId("5450d8af6a092a0b74a44026"), 
            ObjectId("5450d8af6a092a0b74a44026")
        ]
    ],
    "DeletedDate" : [ 
        [ 
            null, 
            null
        ]
    ]
},
"server" : "mongowecntprod:27017",
"filterSet" : false,
"stats" : {
    "type" : "PROJECTION",
    "works" : 2124,
    "yields" : 21,
    "unyields" : 21,
    "invalidates" : 0,
    "advanced" : 2120,
    "needTime" : 0,
    "needFetch" : 2,
    "isEOF" : 1,
    "children" : [ 
        {
            "type" : "KEEP_MUTATIONS",
            "works" : 2124,
            "yields" : 21,
            "unyields" : 21,
            "invalidates" : 0,
            "advanced" : 2120,
            "needTime" : 1,
            "needFetch" : 2,
            "isEOF" : 1,
            "children" : [ 
                {
                    "type" : "FETCH",
                    "works" : 2124,
                    "yields" : 21,
                    "unyields" : 21,
                    "invalidates" : 0,
                    "advanced" : 2120,
                    "needTime" : 1,
                    "needFetch" : 2,
                    "isEOF" : 1,
                    "alreadyHasObj" : 0,
                    "forcedFetches" : 0,
                    "matchTested" : 2120,
                    "children" : [ 
                        {
                            "type" : "IXSCAN",
                            "works" : 2121,
                            "yields" : 21,
                            "unyields" : 21,
                            "invalidates" : 0,
                            "advanced" : 2120,
                            "needTime" : 1,
                            "needFetch" : 0,
                            "isEOF" : 1,
                            "keyPattern" : "{ UserId: 1, ShareId: 1, ParentId: 1, DeletedDate: 1 }",
                            "isMultiKey" : 0,
                            "boundsVerbose" : "field #0['UserId']: [ObjectId('5450d837f32a1e098c844e2a'), ObjectId('5450d837f32a1e098c844e2a')], field #1['ShareId']: [ObjectId('5450d879f32a1e098c844e94'), ObjectId('5450d879f32a1e098c844e94')], field #2['ParentId']: [ObjectId('5450d8af6a092a0b74a44026'), ObjectId('5450d8af6a092a0b74a44026')], field #3['DeletedDate']: [null, null]",
                            "yieldMovedCursor" : 0,
                            "dupsTested" : 0,
                            "dupsDropped" : 0,
                            "seenInvalidated" : 0,
                            "matchTested" : 0,
                            "keysExamined" : 2120,
                            "children" : []
                        }
                    ]
                }
            ]
        }
    ]
}
}

但如果我在没有DeletedDate的情况下进行查询:

db.Files.find({ "UserId" : ObjectId("5450d837f32a1e098c844e2a"),
    "ShareId" : ObjectId("5450d879f32a1e098c844e94"),
    "ParentId" : ObjectId("5450d8af6a092a0b74a44026")},
    {_id:0, ShareId:1}).explain()

然后“indexOnly”是真的.

如何将第一个查询更改为使indexOnly = true?

最佳答案 让我举一个简单的例子,希望能够在你查询字段为空时展示你所看到的内容:

db.nullexplain.find()
{ "_id" : ObjectId("5456759f51a9d5271dc55bba"), "a" : 1 }
{ "_id" : ObjectId("545675a251a9d5271dc55bbb"), "a" : null }
{ "_id" : ObjectId("545675a551a9d5271dc55bbc") }

db.nullexplain.ensureIndex({a:1})

db,nullexplain.count({a:1}).count()
1
db.nullexplain.count({a:null}).count()
2

你看到了这个问题吗?当“a”存在并显式设置为null时,它被索引为null.
当文档中不存在“a”时,它也被索引为null.

当您查询:

db.nullexplain.find({a:null},{_id:0,a:1})
{ "a" : null }
{  }

我们如何从索引中只得出返回文档是否应该将字段“a”设置为null或者该字段是否应该不存在?
答案是我们不能,因此我们必须检查文件本身.

db.nullexplain.find({a:null},{_id:0,a:1}).explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 3,
"nscanned" : 3,
"nscannedObjectsAllPlans" : 3,
"nscannedAllPlans" : 3,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 3,
"server" : "Asyas-MacBook-Pro.local:27017",
"filterSet" : false
}

希望这有助于您了解查询DeletedDate的原因:null必须检查文档并且无法从索引中回答.

点赞