以编程方式更新 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.
我如何做到这一点?谢谢.
How do I accomplish this? Thanks.
推荐答案
您需要正确设置更新 $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屋!