在 MongoDb find() 方法上使用个性化运算符

Working with personalized operators on MongoDb find() method

我遇到了查询结果的问题,我想比较一个字段的值,与其他系统值相乘和比较。

是这样的:

var servicePriceProportion = 0.7;
var serviceMaxPrice = 1000;

        CollectionX.find({
        ... other conditions,
        $where: function(){
            return (this.Data.investmentMax * servicePriceProportion < serviceMaxPrice);
        }
        });

我也试过了。但是没用:

 $where: function(){
                return (this.Data.investmentMax != this.Data.investmentMax);

}

$where 条件根本不起作用。

如果您在 $where 中使用 javascript 变量,您应该使用 obj 关键字引用它们。

$where: "this.Data.investmentMax * " + servicePriceProportion + " < " + serviceProviderMaxPrice"

所以this指的是集合中的实际数据,obj而是指另一个变量。

您应该知道,使用 $where 运算符可能会导致查询非常缓慢。

来自 docs 的注释。

In general, you should use $where only when you can’t express your query using another operator

所以在简历中,如果有一些方法可以在不使用 $where 的情况下实现这一点,那对应用程序来说会很好