The "right way" to architecture voting with Mongoose?
<p>I'm creating a web app using Mongoose/MongoDB to store information that will be voted on. I'll be storing usernames and IP addresses with the vote (so voters can update/modify their votes if desired). </p>

<p><strong>Root Question: What's the best way to securely architecture voting in a Mongoose schema?</strong></p>

<p>Currently, my schema looks like this (simplified):</p>

<pre><code>var Thing = new Schema({
  title: {
    type: String
  },
  creator: {
    type: String
  },
  options: [{
    description: {
      type: String
    },
    votes: [{
      username: {
        type: String
      },
      ip: {
        type: String
      }
    }]
  }]
});

mongoose.model('Thing', Thing);
</code></pre>

<p>While this makes querying the db for any given <code>Thing</code> super easy, it becomes more problematic for security for obvious reasons - I don't want to be returning out usernames and ip addresses to the browser.</p>

<p>The problem is, I'm not sure which is the best/least painful scenario for securely returning <code>Thing</code> data to the browser:</p>

<ol>
<li><p>Loop through each option in <code>Thing.options</code>, then sub-loop through each vote in <code>Thing.options[i].votes</code> to find the vote cast by the user requesting the data, then delete all votes to get rid of other user data. This seems to be very resource intensive, but I couldn't find a way to use indexOf in subarrays (guidance welcome on this one), i.e. <code>Thing.options.votes.indexOf(username)</code> or something to that effect.</p></li>
<li><p>Store vote information in the already-existing <code>User</code> schema, then have to search through all users for vote data and stick it all together every time I want query a single <code>Thing</code>. This also seems inefficient/more resource intensive/more complicated than necessary.</p></li>
<li><p>Create a separate <code>Vote</code> schema that stores the data more conveniently, but then adds another database call (one for the <code>Thing</code>, one for the <code>Vote</code>).</p></li>
</ol>

<p>This problem is somewhat compounded by the fact that there are different ways to vote, with this being the simplest.</p>

<p><em>Research...for posterity's sake:</em></p>

<p><a href="https://stackoverflow.com/questions/342609/storing-votes-in-a-database">This question</a> addresses voting in databases, but for a relational db, not MongoDB/Mongoose.</p>

<p><a href="https://stackoverflow.com/questions/15454441/mongoose-based-app-architecture">This question</a> addresses Mongoose/Node.js app architecture, but nothing about votes.</p>

<p><a href="https://www.npmjs.com/package/mongoose-voting" rel="nofollow noreferrer">This NPM Module</a> adds voting to Mongoose schemas, but doesn't quite fit my needs.</p>

<p><a href="https://www.ibm.com/developerworks/library/wa-nodejs-polling-app/" rel="nofollow noreferrer">This post</a> looks very promising, as the author is sort of doing what I'm describing in point 1 above (see Listing 13 on the author's post), but he still creates a nested loop, starting in line 22 of Listing 13, to loop through each choice/option, then through each vote for each choice/option. </p>