I need to inc
a variable within the case specific object in an array in the profile object in the users object/mongo collection. The case specific object’s name will equal a local variable, and the I want to inc
the variable num
by 1. What would the syntext for this look like?
Advertisement
Answer
I don’t know exactly what your model looks like but here is an example that may help you.
Test=new Mongo.Collection(null); var id=Test.insert({ hashtags:[{ tag:"meteor", count:2 },{ tag:"javascript", count:5 }] }); var tag="meteor"; Test.update({ _id:id, "hashtags.tag":tag },{ $inc:{ "hashtags.$.count":1 } }); console.log(Test.findOne());
In this example, we specify in the query that we’re interested in updating only the array item whose tag matches our local variable, then we use mongodb $ positional operator to increase the item count property accordingly.