0

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!

1
  • 1
    Query performance depends upon many factors, like - indexes, the amount of data, the hardware (RAM, CPU, network, etc.). The query shape can also affect. That said, what is the expected performance you are looking for is not clear. Commented Jul 22, 2022 at 3:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to implement a multi-filter functionality on a specific endpoint in Express.js. Where I check if a field called offerInternships exists in the req.query so it can look like this for example http://localhost:3000/api/resource?offerInternships=true, it can be false either. But, if that req.query didn't exist in the first place, so I'd call db.collection.find({offerInternships:{$in:[true,false]}}) That's why I was asking, but I guess I found a way around to avoid using any filters.

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.