Optimize mongo $in query

I’ve a mongo collection having around 550,000 documents. Document has an array field path on which i have a below query in my java code. This field is indexed.

Problem is ids in below query can go up to 6000 causing the query to take ~8 secs. Tried to use aggregator to bring it down but no luck.

Could some one please guide here what else can be done.

Query query = new Query(Criteria.where("ancestors").is(null).and("path").in(ids));
var data = mongoTemplate.findDistinct(query, "path", Orders.class, String.class);

Hi @Arun_Raj_Singh and welcome to MongoDB community forums!!

Based on the above information shared, could you help me with some details:

  1. A sample document from the collection.
  2. The indexes defined on the documents.
  3. The query that you are trying to perform.
  4. The MongoDB version you are on.
  5. The explain output for the query that you are using.