Home > database >  MongoDB $in operator query performance with boolean values
MongoDB $in operator query performance with boolean values

Time:07-22

Does my query performance get affected if I used

db.collection.find({field:{$in:[true,false]}})

I mean I know it's the same thing like

db.collection.find({})

But I may some cases where it could be like the first query. So, is it going to affect my query performance?

Thanks in advance!

CodePudding user response:

Definitely, the simple find will be faster. Because

  1. It doesn't need to check any index
  2. It doesn't need to do any filter

Whereas the other query

db.collection.find({field:{$in:[true,false]}})

  1. If you have an index on field then the performance will be better but depends on the total amount of data, cluster resources, schema, etc.

  2. If you do not have an index, it will be slower than #1 as it has to do COLSCAN.

Again, these are all theories. You should try it out with different amounts of data in your cluster and benchmark the results as recommended in the documentation.

When do you need db.collection.find({field:{$in:[true,false]}}) in your case?

It would help if you had this where you have documents that do not fall under either true or false. Otherwise, you can use simple find.

  • Related