且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB $gt/$lt 运算符,价格存储为字符串

更新时间:2023-11-11 19:44:22

如果您打算将 $gt 与字符串一起使用,您将不得不使用正则表达式,这在性能方面不是很好.只需创建一个包含价格数值的新字段或将此字段类型更改为 int/double 会更容易.javascript 版本也应该可以工作,如下所示:

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")

因为 js 会在使用前将其转换为数字.但是,索引不适用于此查询.

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