I'm developing a forum on the similarity Stackowerflow and I want to consult you how best to store counters: the number of evaluations, views and selecting the best answer.
I am using Nodejs/MongoDB. Now I have two collections:
Posts:
[{
_id: ObjectId(1),
title: 'Untitled',
content: 'empty'
}]
Votes:[{
_post: ObjectId(1),
_user: ObjectId(...),
vote: 1,
}, {
_post: ObjectId(1),
_user: ObjectId(...),
vote: 1,
}]
When a user votes for a post with ObjectId(1), then sent the following query:
Votes.create({username: ObjectId(1), vote: 1}).exec(cb);
Upon receipt of the list of posts I have to do multiple queries:
1. To query all posts sorted by creation date
2. Request all of assessment for each post and add them to the posts in nodejs.
If you frequently query the list of posts will be permanent problems in terms of counters, I'd like to improve by recounting the saving of the counter directly in the post:
[{
_id: ObjectId(1),
title: 'Untitled',
content: 'empty',
votes: 2
}]
But in this case I need to maintain the integrity of the data and guaranteed to update the relevant counters in the post on the basis of these estimates Votes.
This problem can be solved by embedding assessments in the form of an array and store it in the posts, but MongoDB doesn't like to update large documents.
In General advise a pattern for guaranteed recalculate counters in the post that you can use, queues, versioning, or something else?
show me the post in stackoverflow where there are 100k votes
even if they will, and if they are slow, they can be placed in a special group and treat differently
if you try to store it compactly 100k * 3байта per user = total 300KB - Bianka0 commented on July 9th 19 at 13:28
db.posts.update({'vote.user1': {$exists: false}}, {votes: {$inc: 1}})
the result can be checked was the recording or not, if not passed, it means the collision (double click)
and on the client side, the button is just not active if the user already voted (this goes in a single request by post) - Bianka0 commented on July 9th 19 at 13:34