我正在尝试更新数组中的对象。这是我的结构:
"_id": "ubtQP9EjmxhXS5z98",
"name": "My Data",
"desc": "What songs should I play at my wedding?",
"private": false,
"suggestions": [
{
"name": "Vote 1",
"link": "http://www.website.com/",
"votes": 0
},
{
"name": "Vote 2",
"votes": 0
}
],
"author": "tovd9Win3C3fntgyR",
"createdAt": "2016-01-10T08:36:37.014Z"
这是我的更新代码:
Meteor.methods({
voteup: function(itemIndex, userId){
Polls.update({"_id": "ubtQP9EjmxhXS5z98"}, {
"$inc": {"suggestions."+itemIndex+".votes": 1}
});
}
});
这不起作用。它只是崩溃我的应用程序。
答案 0 :(得分:1)
您需要动态构建查询:
Meteor.methods({
voteup: function(itemIndex, userId) {
var update = {};
update["suggestions."+itemIndex+".votes"] = 1;
Polls.update({"_id": "ubtQP9EjmxhXS5z98"}, {
"$inc": update
});
}
});