在 MongoDB 更新语句中使用变量 [英] Using variables in MongoDB update statement

查看:38
本文介绍了在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆