What is the proper way to calculate post counters with MongoDB based application?
<p>I'm building forum with questions and answers like SO. My database is MongoDB and my question is how to properly store and calculate counters related to post?</p>

<p>For example I have two collections:</p>

<p><strong>Posts:</strong></p>

<pre><code>[{
   _id: ObjectId(1),
   title: 'Untitled',
   content: 'empty'
}]
</code></pre>

<p><strong>Votes:</strong></p>

<pre><code>[{ 
    _post: ObjectId(1),
    _user: ObjectId(...),
    vote: 1,        
}, {
    _post: ObjectId(1),
    _user: ObjectId(...),
    vote: 1,
}]
</code></pre>

<p>When user vote to post with <code>ObjectId(1)</code> he issues the following query:</p>

<pre><code>Votes.create({_post: ObjectId(1), vote: 1}).exec(cb);
</code></pre>

<p>All seem looks fine but when I need to fetch chunk of last 100 posts I get performance issues. The thing is I need to:</p>

<ol>
<li>Query chunk of posts</li>
<li>Query all votes for all posts and merge them by hand.</li>
</ol>

<p>I want to fix performance issues and in Post schema additional field for counting votes:</p>

<pre><code>[{
   _id: ObjectId(1),
   title: 'Untitled',
   content: 'empty',
   votes: 2
}]
</code></pre>

<p>But in this case I heed to somehow support consistency and atomic update Post votes when new vote get created in Votes collection.</p>

<p>Could you advice best practices to support consistency between Posts and Votes collection?</p>

<p>Thanks! </p>

<p><strong>I know that I can use embedding and store votes array within Posts but I don't want because my posts may grow and I may stick with another performance issues</strong></p>