以编程方式更新Mongo和Meteor中的字段 [英] Programmatically updating fields in Mongo and Meteor
问题描述
我有一个要更新的收藏集.该字段是通过编程方式指定的,因此我想执行以下操作:
I have a collection that I'd like to update. The field is given programmatically, so I'd like to do something like this:
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'";
无济于事.
That however does not work and just sets "update_string" to have value {{value}} in the object with _id {{id}} in the Collection. I also tried doing var update_string = "'coordinates.lat'";
to no avail.
我该如何完成?谢谢.
推荐答案
您需要正确设置更新$set
参数中的密钥:
You need to set the key in your update $set
parameter correctly:
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
.
Basically without the modification above, If you used {update_string:value}
you would be setting the value of update_string
, not coordinates.lat
.
这篇关于以编程方式更新Mongo和Meteor中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!