Reputation: 289
This is my function and it does not update the count on the NumStat collection document.
Meteor.methods({
addDocument: function(array){
object = {
numbers : array,
date : new Date()
}
NumArray.insert(object);
for (var i = 0; i < array.length; i++) {
NumStat.update({num : array[i]},{$inc : {count : 1}});
console.log(NumStat.findOne({num : array[i]})); // this throws undefined
}
}
});
if I do something like: NumStat.update({num : 2},{$inc : {count : 1}}); It works perfectly but not something like the example that I motioned !? What is wrong with this method and how can I come to the result that I want ? Increasing the count on the NumStat documents finding the document by array[item]
Solution : It was my bad because the value on the array was a string and the num value on the database was an int. The solution was using parseInt(array[i]). A better solution will be using findAndModify as @Dewfy mentioned.
Upvotes: 0
Views: 38
Reputation: 23614
I don't know JavaScript implementation for Mongo at all, but:
update
for other dialect (Java or C#) doesn't create record but update existing one, so review usage of save
p.s. Instead of loop use findAndModify
with specifying array of IDs to modify - then you could avoid loop and place modification logic to server side
Upvotes: 1