在 nodejs 和 mongodb 字段中使用参数时查找结果为空的查询

find query resulting in empty when using parameters in the field for nodejs and mongodb

我试图找到某个位置附近的用户,其中 field1=No 和 field2=No(这就是 cat 等于 (field2))但是当我尝试 运行 查询时这个returns没有结果

function (currentLoc, radius, cat, db, callback) {
    ...
    var query = db.collection('User').find({
        CurrentLoc: {
            $geoWithin: { $centerSphere: [currentLoc, maxDistance] }
        }, 
        field1: "No",
        cat: "Yes"
    });
}

如果我尝试 运行 使用 field2 而不是参数 cat 的查询,则查询有效并且 returns 结果。不确定为什么它也不能通过传入参数工作

您是在告诉它寻找 属性 'cat'。您需要手动设置查询 属性。参见 How to use a variable as a field name in mongodb-native findOne()?