I have the following code snippet to run the aggregation command.
console.time(\"something\");
const cursor = await db.collection(\"main\").aggregate
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.