Javascript 在mongo db中使用变量作为字段名的一部分?

Javascript 在mongo db中使用变量作为字段名的一部分?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我对mongodb的查询有问题。 首先,我的mongoose模式中有一个对象,如下所示 obj_proof: { field1_proof: String, field2_proof: String } 我想在mongo上做一个查询,其中字段名的一部分会自动更改(实际上它是作为函数的参数传递的) 这是一个例子: var attr = 'obj_proof.'+field; //field is passed by the some function in wh

我对mongodb的查询有问题。 首先,我的mongoose模式中有一个对象,如下所示

obj_proof: {
        field1_proof: String,
        field2_proof: String 
}
我想在mongo上做一个查询,其中字段名的一部分会自动更改(实际上它是作为函数的参数传递的) 这是一个例子:

var attr = 'obj_proof.'+field; //field is passed by the some function in which                there is this code
    ProofSchema.update({ 'Id': Id }, { attr : value }, function(err, result) {  //Id is another parameter that is passed by the same function, like field and value
});
但它不起作用。 问题是我不想复制这个函数,因为obj_证明总是相同的。另一方面,字段会发生变化。

试试这个

var attr = 'obj_proof.' + field; //field is passed by the some function in which                there is this code

var key_attr = {};
key_attr[attr] = value; // the value


ProofSchema.update({'Id': Id}, key_attr, function(err, result) {  //Id is another parameter that is passed by the same function, like field and value
});
谢谢