MongoDB 解释查询计划

标签 mongodb mongodb-query

我有一个带有索引的集合:

{
    "authorizations.participant.participantId" : 1,
    "authorizations.action" : 1
}

我有一个疑问:

db.users.find({
    "$query" : {
        "$and" : [
            {
                "authorizations" : {
                    "$elemMatch" : {
                        "action" : "READ" ,
                        "participant.participantId" : {
                            "$in": ["5549b40444ae1e4a5764fb0a","5549b3f644ae1e4a5764facb"]
                        }
                    }
                }
            }
        ]
    },
    "$explain" : true
})

我试图理解查询执行计划:

{
"cursor" : "BtreeCursor authorizations",
"isMultiKey" : true,
"n" : 22,
"nscannedObjects" : 22,
"nscanned" : 23,
"nscannedObjectsAllPlans" : 22,
"nscannedAllPlans" : 23,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
    "authorizations.participant.participantId" : [ 
        [ 
            "5549b3f644ae1e4a5764facb", 
            "5549b3f644ae1e4a5764facb"
        ], 
        [ 
            "5549b40444ae1e4a5764fb0a", 
            "5549b40444ae1e4a5764fb0a"
        ]
    ],
    "authorizations.action" : [ 
        [ 
            "READ", 
            "READ"
        ]
    ]
},
"allPlans" : [ 
    {
        "cursor" : "BtreeCursor authorizations",
        "isMultiKey" : true,
        "n" : 22,
        "nscannedObjects" : 22,
        "nscanned" : 23,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nChunkSkips" : 0,
        "indexBounds" : {
            "authorizations.participant.participantId" : [ 
                [ 
                    "5549b3f644ae1e4a5764facb", 
                    "5549b3f644ae1e4a5764facb"
                ], 
                [ 
                    "5549b40444ae1e4a5764fb0a", 
                    "5549b40444ae1e4a5764fb0a"
                ]
            ],
            "authorizations.action" : [ 
                [ 
                    "READ", 
                    "READ"
                ]
            ]
        }
    }
],
"server" : "modi:27017",
"filterSet" : false,
"stats" : {
    "type" : "KEEP_MUTATIONS",
    "works" : 23,
    "yields" : 0,
    "unyields" : 0,
    "invalidates" : 0,
    "advanced" : 22,
    "needTime" : 0,
    "needFetch" : 0,
    "isEOF" : 1,
    "children" : [ 
        {
            "type" : "FETCH",
            "works" : 23,
            "yields" : 0,
            "unyields" : 0,
            "invalidates" : 0,
            "advanced" : 22,
            "needTime" : 0,
            "needFetch" : 0,
            "isEOF" : 1,
            "alreadyHasObj" : 0,
            "forcedFetches" : 0,
            "matchTested" : 22,
            "children" : [ 
                {
                    "type" : "IXSCAN",
                    "works" : 23,
                    "yields" : 0,
                    "unyields" : 0,
                    "invalidates" : 0,
                    "advanced" : 22,
                    "needTime" : 0,
                    "needFetch" : 0,
                    "isEOF" : 1,
                    "keyPattern" : "{ authorizations.participant.participantId: 1, authorizations.action: 1 }",
                    "isMultiKey" : 1,
                    "boundsVerbose" : "field #0['authorizations.participant.participantId']: [\"5549b3f644ae1e4a5764facb\", \"5549b3f644ae1e4a5764facb\"], [\"5549b40444ae1e4a5764fb0a\", \"5549b40444ae1e4a5764fb0a\"], field #1['authorizations.action']: [\"READ\", \"READ\"]",
                    "yieldMovedCursor" : 0,
                    "dupsTested" : 22,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0,
                    "matchTested" : 0,
                    "keysExamined" : 23,
                    "children" : []
                }
            ]
        }
    ]
}
}

据我了解,虽然我可以“cursor”:“BtreeCursor授权”,但“nscannedObjects”= 22不是说有一个完整的集合扫描吗? (该集合中有 22 个文档)。

最佳答案

您似乎使用的是旧版本的 MongoDB,从 3.0 开始,explain() 输出看起来会有所不同 --- 只是顺便说一句,因为您没有提及版本号。

您的查询与您的所有 22 个对象相匹配:

"n" : 22,

所以是的,这是一次完整扫描。如果您想要扫描的对象少于您的对象,则必须使用匹配少于全部的查询。

“n”含义的文档: http://docs.mongodb.org/v2.2/reference/explain/#explain-output-fields-core

explain.n

n is a number that reflects the number of documents that match the query selection criteria.

关于MongoDB 解释查询计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30069325/

相关文章:

mongodb - 使用 ssl 身份验证在 scala 中连接 mongo 服务器

mongodb - Meteor - "findOne"函数不再回调

ruby-on-rails - Mongoid `group()` 条件

mongodb - 蒙戈 : Return specific fields from an array slice

mongodb:UnknownError 断言 src/mongo/db/server_options_helpers.cpp:355

node.js - mongodb schema.createIndex 不是一个函数

mongodb - 如何在 mongodb 中使用查找来匹配数组字段?

java - 使用java在mongodb中进行多次更新

node.js - 如果第三个键为 true,则比较 mongodb 文档的两个键

java - Mongodb从数组中删除项目