title: How to pass variable in MongoDB query?
body: <p>I have a mongoDB update function that performs differently according to the key submitted. If I do if-else like below, it works fine:</p>
  <pre><code>if(req.body.nature == 'In') { 
  const user = await db.findOne({ id: 'idNum' });
      user.dailyTime.push({
        in: true
      });
      await user.save();
  }
  
  if(req.body.nature == 'out') { 
  const user = await db.findOne({ id: 'idNum' });
      user.dailyTime.push({
        out: true
      });
      await user.save();
  }
  </code></pre>
  <p>However, as code becomes longer, it becomes error-prone. I have tried to do below:</p>
  <pre><code>if(req.body.nature == 'In') { data = {in: true}}
  if(req.body.nature == 'Out') { data = {out: true}}
  
  const user = await db.findOne({ id: 'idNum' });
      user.dailyTime.push({
        data
      });
      await user.save();
  </code></pre>
  <p>This did not work.</p>