gpt4 book ai didi

javascript - 使用变量作为 mongodb 中字段名称的一部分?

转载 作者:行者123 更新时间:2023-12-02 16:19:49 25 4
gpt4 key购买 nike

我在 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_proof 总是相同的。另一方面,领域发生变化。

最佳答案

试试这个

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
});

谢谢

关于javascript - 使用变量作为 mongodb 中字段名称的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29230819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com