I am using db.collection.countDocument({filter1 : val, filter2 : val2}) it is taking 5 mins to return the result, total records are 206 m after filter it is 80 m
I have created index as well but still no luck also the filter condition will change as per the user inputs.
Please suggest the faster approach to get the count and this will be used in Java spring boot.
I have created regular index on filter1 and another regular index on filter 2
depends on the user its a search screen multiple search box are present on UI application as per user requirement they will enter the search value and hit the search button
The best index for this specific query is the compound index {filter:1,filter:2}, both filter1 and filter2 would have to be matched on, and in that order for the index to be used.
In the sample here there are 3 indexes plus the _id index to begin with. The index I mention above is hidden:
This is a big improvement. This is limited in that the index will support queries on f1 alone and f1,f2 combined, but not queries on f2 alone.
It is possible the the Attribute Pattern may suit this use case, updating the schema to support this could be beneficial. Have a read and see if it fits.
I agree with Chris. Alternatively, you can create two indexes: one compound index on (f1, f2) and a single index on f2. This approach will handle all your queries involving f1, f2, and f1+f2. I don’t think the attribute pattern is necessary for just two fields.