2
votes

I wanted to do a covered aggregation and therefore created an index with all the fields that are used in the query. The problem is that the explain tells me that the query isn't covered.

I just don't know what I am missing here..

That's the command

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

And here's the explain output


    {
        "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

1 Answers

0
votes

You seem to have an array index, as suggested by the "isMultiKey" : true part of the explain. MongoDB cannot cover the query in that case. Check the answer below, seems to be the same case as yours.

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