What would be cheaper query-- querying using $bucketAuto to categorize data and then using $match (with indexes) to filter again OR querying mutiple times for data of different categories (using compound index)?

I am trying to query a collection that has documents of different categories. However, i don’t know which would be a cheaper and efficient expression, using $bucketAuto and $match or quering multiple times but only using $match?

If I haven’t made anything clear do let me know :slight_smile:

I just thought about it. I think the best idea would be use $match before $bucketAuto, which will ensure only the documents I want are sorted into buckets and only one query is needed.

Hey @Nirmal_Bhandari,

Welcome to the MongoDB Community Forums! :leaves:

Glad that you have found the answer you’re looking for, but do you mind sharing why you think it’s the best solution for you?

In general, the scenario you described depends on the size of your collection and the distribution of the categories within it.

If your collection is small, or if the categories are relatively evenly distributed, querying multiple times using $match may be simpler and not should not take up much time too since the collection is small.
On the other hand, if your collection is large, using $bucketAuto and $match may be a better choice. This is because $bucketAuto can automatically group documents based on their values, which can save you the effort of manually querying multiple times.

Please note that these are general pointers in nature and it’s always a good idea to test both approaches with your specific data set and measure their performance. This will help you determine which approach is more efficient for your use case.

Hope this helps. Feel free to reach out for anything else as well.