【问题标题】:Working with personalized operators on MongoDb find() method在 MongoDb find() 方法上使用个性化运算符
【发布时间】:2015-04-09 05:34:51
【问题描述】:

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

是这样的:

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 条件根本不起作用。

【问题讨论】:

    标签: mongodb meteor mongodb-query


    【解决方案1】:

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

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

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

    您应该知道,使用 $where 运算符会使查询变得非常缓慢。

    来自docs 的说明。

    一般来说,只有当你不能表达你的意思时,你才应该使用 $where 使用其他运算符查询

    所以在简历中,如果有某种方法可以在不使用$where 的情况下完成此任务,那对应用程序来说会很好

    【讨论】:

    • 你得到了什么?你得到完整的查询?没有$where 条件?
    • 查看文档这将不起作用。您不能使用嵌套文档,因此请尝试使用 this.investmentMax,因为 this.data.investmentMax 将不起作用/仅将 $where 查询运算符应用于顶级文档。
    • 超出条件 $where 的结果相同。好像 $where 条件不存在。对于测试,使用: $where: "this.Data.investmentMax != this.Data.investmentMax" } );奇怪
    • 所以它似乎没有采用 2 个 javascript 变量对吗?
    • 是的...这是因为“仅将 $where 查询运算符应用于顶级文档。”?有其他解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2014-06-27
    • 1970-01-01
    • 2014-09-26
    相关资源
    最近更新 更多