在MongoDB更新语句中使用变量 [英] Using variables in MongoDB update statement
本文介绍了在MongoDB更新语句中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在更新语句中使用变量作为字段名称,但是它根本不起作用,有什么建议吗?
I am trying to use a variable as the field name in an update statement and it is not working at all, any suggestions?
例如:
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对象:
Javascrip objects can be accessed two ways:
object.attribute
或
object["attribute"]
如果使用第二种方法,则可以使用变量访问它
if you use the second method you can access it with a variable
这篇关于在MongoDB更新语句中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文