title: MongoDB/Node- Check if a document within a collection has a specific field
body: <p>I'm new to node, express and mongodb and I have been looking at documentation and other StackOverflow for a few hours and still haven't managed to figure this out.</p>
  
  <p>I'm using the MongoDB node.js driver.</p>
  
  <p>I am trying to create a voting app where people can only vote once per poll. To do this I store their IP in the question document in the questions collection:</p>
  
  <pre><code>  var questionId = ObjectId(req.body._id);
  
    var ip = req.headers['x-forwarded-for'] ||
         req.connection.remoteAddress ||
         req.socket.remoteAddress ||
         req.connection.socket.remoteAddress;
    ip = ip.replace("::ffff:", "").replace(/\./g, "");
  
    db.collection('questions')
    .findOneAndUpdate({"_id": questionId}, {$set : {[ip] : true} }, (err, result) =&gt; {
      if (err) return res.send(err)
    }) 
  </code></pre>
  
  <p>However, I want to verify that the IP does not already exist in the question's document.``</p>
  
  <p>So I want to check if <code>{'_id': questionId}</code> has: <code>{[ip]: true}</code> or even <code>ip</code> as a field. </p>
  
  <p>The database entry looks like this (edited for clarity):</p>
  
  <pre><code>{
      "_id": {
          "$oid": "58caeb3402f11c2d66dc3f41"
      },
      "question": "Metallica or Megadeth?",
      "answer1": "Metallica",
      "answer2": "Megadeth",
      "answer1_votes": 14,
      "answer2_votes": 11,
      "127001": true
  }
  </code></pre>
  
  <p>As we can see 127.0.0.1 has voted already, so I would want to stop that user voting again on that question.</p>
  
  <p>I'm doing it with the IP as I want non-registered users to be able to vote.</p>
  
  <p>I'm sure it's very simple, but I'm lost completely.</p>