MongoDB:虽然只使用索引字段,但不包括聚合查询

时间:2014-02-13 09:55:25

标签: mongodb aggregation-framework

我想进行覆盖聚合,因此创建了一个包含查询中使用的所有字段的索引。问题是解释告诉我查询没有被覆盖。

我只是不知道我在这里失踪了什么..

那是命令

db.tickets.runCommand(
  'aggregate', 
  {pipeline: [
    {$match:{tags:'unread'}},
    {$group:{
      _id:{acc:'$accountId',cha:'$channelId',ass:'$assignee'}, 
      count:{$sum:1}}}], 
  explain: true
})

这是解释输出


    {
        "serverPipeline" : [ 
            {
                "query" : {
                    "tags" : "unread"
                },
                "projection" : {
                    "accountId" : 1,
                    "assignee" : 1,
                    "channelId" : 1,
                    "_id" : 0
                },
                "cursor" : {
                    "cursor" : "BtreeCursor tags_1_accountId_1_channelId_1_assignee_1",
                    "isMultiKey" : true,
                    "n" : 18093,
                    "nscannedObjects" : 18093,
                    "nscanned" : 18093,
                    "nscannedObjectsAllPlans" : 18093,
                    "nscannedAllPlans" : 18093,
                    "scanAndOrder" : false,
                    "indexOnly" : false,
                    "nYields" : 13,
                    "nChunkSkips" : 0,
                    "millis" : 61,
                    "indexBounds" : {
                        "tags" : [ 
                            [ 
                                "unread", 
                                "unread"
                            ]
                        ],
                        "accountId" : [ 
                            [ 
                                {
                                    "$minElement" : 1
                                }, 
                                {
                                    "$maxElement" : 1
                                }
                            ]
                        ],
                        "channelId" : [ 
                            [ 
                                {
                                    "$minElement" : 1
                                }, 
                                {
                                    "$maxElement" : 1
                                }
                            ]
                        ],
                        "assignee" : [ 
                            [ 
                                {
                                    "$minElement" : 1
                                }, 
                                {
                                    "$maxElement" : 1
                                }
                            ]
                        ]
                    },
                    "allPlans" : [ 
                        {
                            "cursor" : "BtreeCursor tags_1_accountId_1_channelId_1_assignee_1",
                            "n" : 18093,
                            "nscannedObjects" : 18093,
                            "nscanned" : 18093,
                            "indexBounds" : {
                                "tags" : [ 
                                    [ 
                                        "unread", 
                                        "unread"
                                    ]
                                ],
                                "accountId" : [ 
                                    [ 
                                        {
                                            "$minElement" : 1
                                        }, 
                                        {
                                            "$maxElement" : 1
                                        }
                                    ]
                                ],
                                "channelId" : [ 
                                    [ 
                                        {
                                            "$minElement" : 1
                                        }, 
                                        {
                                            "$maxElement" : 1
                                        }
                                    ]
                                ],
                                "assignee" : [ 
                                    [ 
                                        {
                                            "$minElement" : 1
                                        }, 
                                        {
                                            "$maxElement" : 1
                                        }
                                    ]
                                ]
                            }
                        }
                    ],
                    "oldPlan" : {
                        "cursor" : "BtreeCursor tags_1_accountId_1_channelId_1_assignee_1",
                        "indexBounds" : {
                            "tags" : [ 
                                [ 
                                    "unread", 
                                    "unread"
                                ]
                            ],
                            "accountId" : [ 
                                [ 
                                    {
                                        "$minElement" : 1
                                    }, 
                                    {
                                        "$maxElement" : 1
                                    }
                                ]
                            ],
                            "channelId" : [ 
                                [ 
                                    {
                                        "$minElement" : 1
                                    }, 
                                    {
                                        "$maxElement" : 1
                                    }
                                ]
                            ],
                            "assignee" : [ 
                                [ 
                                    {
                                        "$minElement" : 1
                                    }, 
                                    {
                                        "$maxElement" : 1
                                    }
                                ]
                            ]
                        }
                    },
                    "server" : "NODE50:27017"
                }
            }, 
            {
                "$group" : {
                    "_id" : {
                        "acc" : "$accountId",
                        "cha" : "$channelId",
                        "ass" : "$assignee"
                    },
                    "count" : {
                        "$sum" : {
                            "$const" : 1
                        }
                    }
                }
            }
        ],
        "ok" : 1
    }

1 个答案:

答案 0 :(得分:0)

您似乎有一个数组索引,正如解释的"isMultiKey" : true部分所示。在这种情况下,MongoDB无法覆盖查询。检查下面的答案,似乎与你的情况相同。

https://stackoverflow.com/a/14191091/2619107