What you want is the $elemMatch
operator. This allows you to select array elements that require multiple fields to match a condition:
db.users.update(
{ "groups": {
"$elemMatch": {
"match_id": "d4f14940",
"score": [2,1]
}
}},
{ "$inc": { "points": 1 } }
)
So that allows you to match the document as the "groups" array contains an element that has both match_id
and score
that meet the conditions you supplied. If you change that match_id
without altering the score, then the document does not match and the increment is not made to your points
value.
So this has nothing to do with the positional $
operator in this case as you are not updating and array at a matched position, which is what that is for.