我正在尝试使用相同的代码增加我的集合中的不同值。
我试图在一个函数中计算它,其中更新的属性将取决于参数:
BuyEntitee : function(id_fb,entitee)
{
var j = Joueur.findOne({id_fb:id_fb});
var prix_current = j["prix_"+entitee];
var update_query = {};
update_query[entitee+"s"] = j[entitee+"s"] + 1;
update_query["prix_"+entitee] = Math.round(50*Math.pow(1.1,j[entitee+"s"]));
Joueur.update( {id_fb:id_fb}, {
$set: {update_query}
}
);
j = Joueur.findOne({id_fb:id_fb}); // on recharge le jouer ... utile ??
console.log('nbHumains : ' + j.humains+ ' query = '+JSON.stringify(update_query));
return j.rochers;
}
但不幸的是,查询在结构中太深了 1 层:
meteor:PRIMARY> db.joueur.findOne()
{
"_id" :
"humains" : 12,
"prix_humain" : 50,
"update_query" :
{
"humains" : 13,
"prix_humain" : 157
}
}
我正在创建 update_query
对象,以便我可以以编程方式更改更新函数中的参数(参见此 here )。
有办法实现吗?
最佳答案
事实上,发生的事情是用于指定对象的 ES6 语法糖的结果。
当您指定{update_query}
时,它被解释为键为 "update_query"
的对象和变量的值update_query
.
因此,它相当于:
Joueur.update( {id_fb:id_fb}, {
$set: {
update_query: update_query
}
}
);
你真正想要的是分配 update_query
本身到$set
关键:
Joueur.update( {id_fb:id_fb}, {
$set: update_query
}
);
顺便说一句,您可能想使用 $inc
修饰符以增加 entitee+"s"
增加 1。
关于javascript - 如何更新作为参数接收的多个集合键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33726910/