我正在尝试从mongo shell更新运行JavaScript脚本的名为“ things”的文档。当我明确地说出要更新的字段时,它就像一个超级按钮,但是当我尝试从数组中读取字段名称时遇到问题。
这是我的代码:
var continuousWithMissingValues = ["A2","A14"];
var categoricWithMissingValues = ["A1", "A4", "A5", "A6", "A7"];
for (var i = 0; i < continuousWithMissingValues.length; i++){
// db.things.update({A2: "?" },{$set: {A2: -1 }}, { multi : true }); this line is working properly
db.things.update({continuousWithMissingValues[i]: "?" },{$set: {continuousWithMissingValues[i]: -1 }}); //if I try to update while reading values from the array I'm getting error
print('Updated missing values for'+ continuousWithMissingValues[i]);
}
我也尝试过使用此行而没有任何运气(这不会产生错误,但不会更新任何内容):
db.things.update({"continuousWithMissingValues.i": "?" },{$set: {"continuousWithMissingValues.i": -1 }});
答案 0 :(得分:0)
mongo shell不支持computed property names,因此您必须按照以下步骤建立查询并更新对象:
var query = {};
query[continuousWithMissingValues[i]] = "?";
var update = {$set: {}};
update.$set[continuousWithMissingValues[i]] = -1;
db.things.update(query, update);