【问题标题】:Programmatically updating fields in Mongo and Meteor以编程方式更新 Mongo 和 Meteor 中的字段
【发布时间】:2014-03-12 00:54:57
【问题描述】:

我有一个想要更新的收藏。该字段以编程方式给出,所以我想做这样的事情:

var update_string = 'coordinates.lat';
var update = function(value, id, update_string) {
  Collection.update({_id:id}, {$set:{update_string:value}})
}  

但这不起作用,只是将“update_string”设置为在集合中具有 _id {{id}} 的对象中具有值 {{value}}。我也试过 var update_string = "'coordinates.lat'"; 无济于事。

我该如何做到这一点?谢谢。

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    你需要在你的更新$set参数中正确设置key:

    var update = function(value, id, update_string) {
       var update_query = {};
       update_query[update_string] = value 
       Collection.update({_id:id}, {$set:update_query})
    }  
    

    基本上没有上面的修改,如果你使用{update_string:value},你将设置update_string的值,而不是coordinates.lat

    【讨论】:

    • 哦,有道理。谢谢阿克沙特。我回来后会试试这个,会给你信任的。
    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2021-10-05
    • 2011-06-22
    • 2021-08-22
    相关资源
    最近更新 更多