Suppose that I querying by an arbitrary field category
that will be four characters long with all numbers.
So, it could be '1234'
, '2341'
, '9999'
etc.
Now, if I am certain that this numerical representation of this string will be greater than or equal to 1000 and less than or equal to 9999, does making this field a number
instead of a string
make the query more efficient? I believe that number comparisons are more efficient than string comparisons, but I am not sure that it will have any discernible effect on the query performance. I believe that making this field a string
would be better if query performance is equal because the field categories number is just an identifier and therefore a string is more descriptive of what is going on, but better query performance is more important.
Therefore, my question is if I query on this arbitrary category
field will it be more efficient to query by string or by number or that it doesn't matter?
Comparing two strings is very slow and expensive. Most algorithms require iterating through entire string and matching each character.
You must apply limit() to the cursor before retrieving any documents from the database. Use limit() to maximize performance and prevent MongoDB from returning more results than required for processing.
Other ways to improve MongoDB performance after identifying your major query patterns include: Storing the results of frequent sub-queries on documents to reduce read load. Making sure that you have indices on any fields you regularly query against. Looking at your logs to identify slow queries, then check your indices.
According to this and that index is represented as BSON document in binary form.
Efficiency can be gained by making it small (index just what is needed) rather than playing with data types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With