Missing 'winningPlan' while using 'explain()' with query in MongoDB

I’ve configured a search index on my collection and implemented the following queries:

Query 1:

db.productDimensions.explain().aggregate([{
    $search: {
      "text": {
        "path": "name",
        "query": "productName"
      }
    }
  },
  {$sort: { name: 1 }}
]);

Query 2:

db.productDimensions.explain("executionStats").aggregate([{
  "$search": {
    "text": {
      "path": "name",
      "query": "productName"
    },
    "sort": {
      unused: {$meta: "searchScore"},
      "name": 1,
    }
  }
}]);

Upon executing these queries with explain(), I observed that the winningPlan field was absent. How can I verify whether the search index is being utilized? My understanding is that the winningPlan typically indicates whether the index is utilized. If this assumption is incorrect, how can I determine if the search index is indeed being used?

Hello Samrat,

The reason why the winningPlan field is not present in the output just so happens to be because the search index is being utilized exclusively. The only query stage to be explained is the query running against the mongot process, hence why the output of the first stage in the explain output is "$_internalSearchMongotRemote" . $search aggregation pipeline stages are executed against the mongotprocess, which is technically a Lucene index. So if you look at the fields $_internalSearchMongotRemote.mongotQuery.index you can specifically find the search indexes being used.

winningPlan is output from the mongod process query planner, which would only be observed in the output of .explain() for things like .find() and other .aggregate() pipeline stages that are not $search which need to query and fetch documents in a MongoDB collection, or utilize an index (not to be confused with a search index).

We could probably include something in our docs that better explains this, as currently there are two different docs for explain results that don’t really allude to the fact that different output can be expected when using $search exclusively.

Current docs:
Interpret Explain Plan Results

Search Explain

1 Like

@Sam_Wickline Thanks, this will be beneficial for me.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.