1

有人看到这个 mongoDB 查询不应该是 indexOnly: true 的原因吗?

我希望我犯了一个愚蠢的错误......我看不出我做错了什么以及为什么这不是一个涵盖的查询。

我正在使用 MongoDB 2.4.6

以下是重现的步骤:

db.Test.insert({"_id" : ObjectId("5261ac33e4b070ca9e1480d1"), "date" : ISODate("2013-10-18T21:46:27.476Z"), "disabled" : false, "userHidden" : false, "postId" : NumberLong(103)})

db.Test.ensureIndex({ "postId" : 1, "userHidden" : 1, "disabled" : 1, "date" : -1}, {name: "findByPostId", sparse: true, unique: false})

db.Test.find( { $query: {postId: 103, userHidden: false, disabled: false}, $orderby: {date: -1}, $hint: "findByPostId", $explain: 1 }, {postId: 1, userHidden: 1, disabled: 1, date: 1 }  ).pretty()

{
    "cursor" : "BtreeCursor findByPostId",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 1,
    "nscannedAllPlans" : 1,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
            "postId" : [
                    [
                            103,
                            103
                    ]
            ],
            "userHidden" : [
                    [
                            false,
                            false
                    ]
            ],
            "disabled" : [
                    [
                            false,
                            false
                    ]
            ],
            "date" : [
                    [
                            {
                                    "$maxElement" : 1
                            },
                            {
                                    "$minElement" : 1
                            }
                    ]
            ]
    },
4

1 回答 1

0

我想到了。希望这会对其他人有所帮助。

这就是问题所在: http: //docs.mongodb.org/manual/tutorial/create-indexes-to-support-queries/#indexes-covered-queries

默认情况下,MongoDB 在查询结果中包含 _id 字段。因此,如果索引不包含_id 字段,那么您必须从查询结果中排除_id 字段(即_id:0)。

通过将 _id: 0 添加到投影中,我得到 indexOnly true

> db.Test.find( { $query: {postId: 103, userHidden: false, disabled: false}, $orderby:     {date: -1}, $hint: "findByPostId", $explain: 1 }, {_id: 0, postId: 1, userHidden: 1, disabled: 1, date: 1 }  ).pretty()
{
    "cursor" : "BtreeCursor findByPostId",
    "isMultiKey" : false,
    "n" : 3,
    "nscannedObjects" : 0,
    "nscanned" : 3,
    "nscannedObjectsAllPlans" : 0,
    "nscannedAllPlans" : 3,
    "scanAndOrder" : false,
    "indexOnly" : true,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
            "postId" : [
                    [
                            103,
                            103
                    ]
            ],
            "userHidden" : [
                    [
                            false,
                            false
                    ]
            ],
            "disabled" : [
                    [
                            false,
                            false
                    ]
            ],
            "date" : [
                    [
                            {
                                    "$maxElement" : 1
                            },
                            {
                                    "$minElement" : 1
                            }
                    ]
            ]
    }
于 2013-10-18T23:43:59.093 回答