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.
JavaScript
x
25
25
1
Test=new Mongo.Collection(null);
2
3
var id=Test.insert({
4
hashtags:[{
5
tag:"meteor",
6
count:2
7
},{
8
tag:"javascript",
9
count:5
10
}]
11
});
12
13
var tag="meteor";
14
15
Test.update({
16
_id:id,
17
"hashtags.tag":tag
18
},{
19
$inc:{
20
"hashtags.$.count":1
21
}
22
});
23
24
console.log(Test.findOne());
25
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.