Meteor Collection.find在动态mongo查询中使用变量作为字段

Mos*_*she 2 mongodb meteor

我试图使用变量作为Collection.find查询中的字段说明符,但山雀只是忽略它

    var qry = "{\"" + field_name + "\":" + field_value + "}"
    console.log(qry)//  {"customer_active":true}
    Customers.find(qry).map(function(customer){// doesn't find anything
        console.log(customer)
        var groups = customer.customer_group_id.push(a._id)

        Customers.update({$set: {customer_group_id: groups}})
    })
Run Code Online (Sandbox Code Playgroud)

如何构建动态查询

Joh*_*yHK 8

qry需要是一个对象,而不是一个字符串.所以建立它像这样:

var qry = {};
qry[field_name] = field_value;
Run Code Online (Sandbox Code Playgroud)