MongoDB $gt/$lt operators with prices stored as strings

If you intend to use $gt with strings, you will have to use regex, which is not great in terms of performance. It is easier to just create a new field which holds the number value of price or change this field type to int/double. A javascript version should also work, like so:

db.products.find("this.price > 30.00")

as js will convert it to number before use. However, indexes won’t work on this query.

Leave a Comment