cailinanne
cailinanne

Reputation: 8372

Is it more efficient to query MongoDb for key=null or for exists=false?

I have a MongoDb collection with some keys that may not be populated relevant for all objects in the collection. I will want to do queries that find all objects where these keys are NOT populated.

Would it be better to

I understand that I can query for the results I want either way (see here), I'm just wondering which is considered "best practice" and why.

Note (in case it's relevant) that the collection has around 10 "required" fields and 3 of these "optional" fields.

Upvotes: 1

Views: 852

Answers (2)

Pavel Veller
Pavel Veller

Reputation: 6115

I would say it depends on the semantics of what your app does with the documents in this collection. You will save yourself some space by not storing the y: null. If you do store the y: null you will have to remember that y technically exists thus you should query for nulls in a certain way. If the layer above Mongo doesn't care then I would go for not storing empty field, just like @Derick recommended. If it does care, however, and you find it easier to work with the nulls in your ys then go for it.

UPDATE To your question about the effectiveness. If you're doing everything right then you're working data set fits in RAM. With that said, I don't think there's a noticeable difference in either approach unless you index by y. In this case the behavior may be different but the best way to find out is to run some tests.

Upvotes: 1

Derick
Derick

Reputation: 36774

I would go for not storing empty fields. However, it depends a little bit on what sort of queries you're going to run on this collection. If you can elaborate on that, I'll update the answer.

Upvotes: 1

Related Questions