2

This article explains very clearly how to implement a voting system with MongoDB, and to limit one vote per user and per object.

I have one extra requirement. I need the votes of a given user to be visible for the objects displayed. For example, if I am displaying 20 tweets, and the user has voted on 3 of those tweets, I want those votes to be visible. (For example, using a green up-arrow.)

One solution is to send to the client, for each question, the set of voters. Another solution is to send to the client the set of votes he has cast. I do not see either solution as a scalable one. Any suggestions?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • I just implemented this for a project a couple months back, describe your data model a bit in the original question and I'll help you write the queries. – Tyler Brock Jan 09 '12 at 18:02

2 Answers2

1

This is something you would do client side.

Once you have the object that contains the vote cound and the array of voters you can check to see if the current user's id is within the array of voters while you iterate over the set of (stories, tweets, what have you)

Does that make sense?

Tyler Brock
  • 29,626
  • 15
  • 79
  • 79
  • So you're saying that for every object I should send down the pipe all the voters. What if there are 100s of voters per object? This seems like a waste of bandwidth. – Randomblue Jan 09 '12 at 20:11
  • Ok, well the other way would be to get the tweets in one query, then make another query for the set of tweet object ids that are by the current user. Then while you iterate over the set of tweets check to see if the id of the current tweet is in the set of that person's tweets. – Tyler Brock Jan 09 '12 at 21:32
  • Cool. The problem with that is, eventually the list of person's tweets will be longer than the list of voters per tweet. That's why I suggested the first method. – Tyler Brock Jan 10 '12 at 00:15
0

Not a full answer, but a link to a good voting library (fast!!!) for ruby/mongoid. Should be easily portable to node.js, perhaps mongoose.

https://github.com/vinova/voteable_mongo

I need something similar eventually, perhaps we should chat (I am martin_sunset on node.js on freenode)

Martin Wawrusch
  • 790
  • 1
  • 6
  • 12