Indexes and $graphLookup

Hello, enjoying the M121 course material so far.

The principal reason I am taking the course is to learn more about how indexes are utilized in the aggregate pipeline generally and in $graphLookup and $lookup specifically.

Starting with $graphLookup. The materials indicate that the field referenced in the connectToField should be indexed. That makes sense. I have the following questions:

  1. Is it possible to have covered queries of the proper compound indexes exist along with a $project stage subsequent to the $graphLookup stage?
  2. Will indexes be used if restrictSearchWithMatch is specified and the fields in that query are also properly indexed?
  3. How does one go about determining what indexes are being utilized by a $graphLookup operation? My experience has been that .explain(‘executionStats’) does not produce useful information beyond indexes that might be used by the initial $match stage.

Have you tried {explain:true} aggregate query option?

@waynesi I just compared the results of the explain option vs .explain(‘executionStats’). The results are roughly the same.

So we see there are details about index use regarding the first match stage, but nothing for the $graphLookup stage.


There’s an open ticket to improve the explain output for both $lookup and $graphlookup:

Will discuss your other two questions with the team to be 100% sure before posting an answer.

José Carlos

@jcarlosgarcia Thank you for the response. I note that there has been no activity on that ticket since 2016. Are there some reliable methods to evaluate index usage in $lookup and $graphLookup stages now?


No, as far as I know the only way is using explain.

José Carlos

You can also check for index usage by using the $indexStats aggregation stage. This is a manual way of seeing what your index hit numbers are.