Why do we need to think of aggregation as a filter not find?

You should explain it if you say so.

Hello @Eugene_Bos wellcome to the forum :wave:

to quote @Asya_Kamsky:

Aggregation is the new find

In case you’d like to like to discuss in deeper detail, could you please elaborate on the background of your question?


Background is “saying words without meaning and not explaining anything”
he doesnt say its new find


I assume you are referring to a particular video in the course. I will have a stab at answering it, though I am sure someone one correct me :slight_smile:

With a find, you return one or many items, and that’s all you do based upon the $match.

However in a $match in a aggregation pipeline, you are merely filtering the items before proceeding to transform that data in other ways, e.g. group, sort, lookup. With all of these stages are happening on the mongo server.


Yes, https://youtu.be/J3GrISCyK-8?t=41 I think they remove links to own videos here.
I see, thanks, so it actually finding results, as it uses indexes, filter would scan all records without using indexes, as I thought they suggest by “thinking as a filter”

The find method just about retrieves all documents, filters based upon the provided filter, applies provided projection on the resulting documents - and returns a cursor on which further operations like sort can be applied.

On the other hand, aggregation has stages, where at each stage different operation can be performed. $match is one such stage. $match performs a filter operation. So, aggregation is not just a filter - but it can be just a filter only. For example,

  { $match: { "author": "Mark Twain" } }

the aggregation query returns documents with the filter { "author": "Mark Twain" }. And, it is same as: db.books.find( { "author": "Mark Twain" } ). As such an index, if defined on the field author, is applied on both the queries in the same way.

1 Like

The $match in both the find and aggregation pipeline, use indexes, as long as the $match is used in the 1st stage of the aggregation pipeline (Though $sort can use the index as well).

Which is why they bang on about filtering your data in the aggregation pipeline, as early as possible. It reduces your possible dataset, while access to the collection indexes are still available.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.