DEV Community

Omar Hussain
Omar Hussain

Posted on

Optimize Mongodb aggregates

NoSql queries can quickly become time consuming if queries rely on relating data across collections with large datasets.

You may be running into query time outs trying to generate a report on data.

In order to minimize the data query has to look through(hence time) do these two steps(if applicable) on every stage

$project

$project only fields that will be needed in proceeding stages.

$match

Narrow down data before $lookup and $group or after $unwind using $match

Yep thats it. If there is anything you do to optimize your aggregate piplines or know of a better way. Teach me!

Discussion (0)