If mongodb aggregate does not use indexes for $lookup why does my performance increase when using indexes?

前端 未结 1 507
借酒劲吻你
借酒劲吻你 2021-01-24 10:15

I have the following code snippet to run the aggregation command.

      console.time(\"something\");
      const cursor = await db.collection(\"main\").aggregate         


        
相关标签:
1条回答
  • 2021-01-24 10:31

    Unfortunately the MongoDB manual doesn't currently mention potential index usage for $lookup, but this is definitely the case.

    A simple $lookup query similar to your example performs an equality match on the foreignField in another collection, so you've added the correct index to improve performance (assuming this field also is reasonably selective).

    As at MongoDB 4.0 the index usage for $lookup is not reported in aggregation explain output. There is a relevant issue to watch/upvote in the MongoDB issue tracker: SERVER-22622: Improve $lookup explain to indicate query plan on the "from" collection.

    0 讨论(0)
提交回复
热议问题