2016-01-10 84 views
0

我正在嘗試更新數組內的對象。這裏是我的結構:基於索引更新數組流星

"_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} 
    }); 
    } 
}); 

這是行不通的。它只是使我的應用程序崩潰。

回答

1

你需要來動態建立這樣的查詢:

Meteor.methods({ 
    voteup: function(itemIndex, userId) { 
    var update = {}; 
    update["suggestions."+itemIndex+".votes"] = 1; 
    Polls.update({"_id": "ubtQP9EjmxhXS5z98"}, { 
     "$inc": update 
    }); 
    } 
});