coder.Shao
coder.Shao

Reputation: 41

Why aggregate+sort is faster than find+sort in mongo?

I'm using mongoose in my project. When the number of documents in my collection becomes bigger, the method of find+sort becomes slower. So I use aggregate+$sort instead. I just wonder why?

Upvotes: 4

Views: 3745

Answers (1)

Clement Amarnath
Clement Amarnath

Reputation: 5466

Without seeing your data and your query it is difficult to answer why aggregate+sort is faster than find+sort.

But below are the things that holds good on find and aggregate

  • A well indexed(Indexing that suits your query) data will always yield faster results on your find query.
  • The components of aggregation pipeline which you use on your aggregate query, more operations is directly proportional to more execution time.
  • When you go for aggregation pipeline you can create new fields such as sum, avg and so on, which is not possible in a find.

see this thread for more info

MongoDB {aggregation $match} vs {find} speed

Upvotes: 4

Related Questions