I have some values in a collection in a document. For example, my key range can be:
10 - 2930 - 3940 - 49
Those are all strings. I want to somehow search (in MongoDB) for strings starting with 10 and ending with 49. How can I do this?
I know I should have these as numbers, but that’s not possible due to the structure of how the system was created.
Thanks
You’re right, best option would have been to model these as numerals. However, my best (only) alternate suggestion would be to use a regex search.
See here for the docs on regex search for mongo and here for creating range expressions.
Depending on possible range, your query would be something like
which matches your range options specified
[10 - 29, 30 - 39, 40 - 49]. Note this would also match49 - 10for example, so you would need to harden that expression up if there are any exclusions not mentioned in the question.Note regex queries come at a performance cost, remodeling and using built in
$gt $ltfunctions would certainly be desirable.Good luck!