1

We've a collection with two fields ( A and B ), both have an index.

Getting the disctinct on any of them is amazingly quick. But the actual query is a disctinct in A with a filter on the other field, B.

db.getCollection('Collection').distinct( "A" , { "B" : "b1" }  )

This is really, really slow as it's scanning the collection ( unfortunatly b1 is not filtering more than a 50% ). Is there a way to make this quicker in MongoDB ?

2 Answers 2

1

According to this JIRA ticket what you can do is just create a composite index like this:

db.collection.createIndex({ "B": 1, "A:" 1 })
Sign up to request clarification or add additional context in comments.

Comments

1

I recommend you to use compound index to solve this problem. As priority is important in compound index based on you query, I recommend you to use this index {A:1, B:1}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.