Compound index with queries that filter for multiple values of a prefixed field

db.col_name.createIndex({"A": 1, "B", 1})
db.col_name.find({A: {$in: [1, 2, 3]} B: 50})

Since three values are selected for the prefix index, would that invalid the compound index?

If not, how will it solve this problem?

Hi @Big_Cat_Public_Safety_Act ,

Please have a look at the explain results as suggested in Skipping fields when querying collections with compound indexes - #3 by Stennie

Explaining queries will show the information you’re looking for, including the effects of adding additional or different indexes. That will also help you get faster answers as you explore indexing.

I also recommend watching Tips and Tricks for Query Performance: Let Us .explain() Them - YouTube from the 2020 conference.

For a more comprehensive (and free) introduction I recommend taking some of the free online courses at MongoDB University. M001: MongoDB Basics is the usual starting point.


1 Like