我试图在更新语句中使用变量作为字段名称,但它根本不起作用,有什么建议吗?
例如:
COLLECTIONNAME.update(
{ _id: this._id },
{ $set: { VARIABLE1 : VARIABLE2 } }
);
实际代码:
'blur .editable' : function () {
var target = event.currentTarget.value;
var field = event.currentTarget.name;
field = ' " ' + field + ' " ';
Hostings.update( { _id: this._id },{ $set: { field : target } } );
}
最佳答案
你可以这样做:
'blur .editable' : function () {
var target = event.currentTarget.value;
var field = event.currentTarget.name;
var obj = {};
obj[field] = target;
Hostings.update( { _id: this._id },{ $set: obj } );
}
可以通过两种方式访问 Javascrip 对象:
object.attribute
或
object["attribute"]
如果您使用第二种方法,您可以使用变量访问它
关于javascript - 在 MongoDB 更新语句中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18503656/