I have a collection with documents of this format:
{
"_id" : ObjectId("51b1e27e31b1f4fe0700001b"),
"proposals" : [
{
"id" : 17,
"type" : "question",
"fr" : {nothing useful},
"en" : {nothing useful},
"vote_count" : 0,
"validate" : 0,
"username" : "username",
"voters" : [ ],
"creationDate" : ISODate("2013-07-25T08:32:40.328Z")
},
{other proposals of the same type}
]
},
{same format}
I'm trying to update the proposal matched by an ID that I receive, in the right parent. I have found a request on the Mongo cookbook that I used successfully, but on less complicated data format, and I can't make it works now. Here it is.
client.collection('games').update({_id: gameId, 'proposals.id': eventId, 'proposals.voters': {'$ne': user}}, {'$push': {'proposals.voters': user}, '$inc': {'proposals.vote_count': 1}, '$set': {'proposals.validate': 1}});
gameId and eventId are in the right format. If I use the query part of the update in a find(), I get the right game. But I'm struggling to see why my document isn't updated.
You must you $ operator to identify what position in array you want update. Your update command will be:
client.collection('games')
.update(
{
_id: gameId,
'proposals.id': eventId,
'proposals.voters': {'$ne': user}
},
{
'$push': {'proposals.$.voters': user},
'$inc': {'proposals.$.vote_count': vote},
'$set': {'proposals.$.validate': 1}
}
);