mongodb - Mongo data modeling/updates for voting (up and down) -
there example on voting data model/update queries in mongo:
http://www.mongodb.org/display/docs/mongodb+data+modeling+and+rails#mongodbdatamodelingandrails-atomicupdates
however need both , down votes (basically, 1 person can either cast vote or down vote). also, want voter able change mind , change upvote downvote or vice-versa (so list of voters , total number not fit).
what best data model , corresponding update call?
i see 2 possibilities, either
'votes': [{ 'user_id' : ... , 'vote': ±1 }]
or
'upvoters': [...], 'downvoters': [...]
but can't make update query first 1 yet, , second 1 looks bit weird (though may me).
first schema looks good. second schema hard because when user click upvote , downvote need add userid 'upvoters' 'downvoters' , remove 'upvoters' , vice versa.
i suppose votes nestead collection of document(suppose questions).
db.questions.update({votes.userid: .. },{ $set : { votes.$.vote : 1 } });//upvote db.questions.update({votes.userid: .. },{ $set : { votes.$.vote : -1 } });//down
and seems need create field inside of questions collection calculate sum of up/down votes:
db.questions.update({_id: .. },{ $inc : { votescount : 1 } }); //up vote db.questions.update({_id: .. },{ $inc : { votescount : -1 } }); // down vote
if need add new user array of votes use
Comments
Post a Comment