title: How can I keep Consistency of two operations in one function with nodejs, express and mongoose
body: <p>Recently, I write a nodejs application with express and mongoose.</p>
  
  <p>When I update two collections in one function, the problem comes up.</p>
  
  <p>Code segment is shown below.</p>
  
  <pre><code>exports.doVote = async (req, res) =&gt; {
    // some codes...
  
    // Operation 1
    const voteNumInfo = await VoteNumInfo.findOneAndUpdate({ activityId, vodId },
      { vodId, vodName, activityId, categoryId, categoryName, $inc: { voteNum: 1, showNum: 1 } },
      { new: true, upsert: true, setDefaultsOnInsert: true });
  
    // Operation 2
    await VoteInfo.create({ vodId, vodName, activityId, categoryId, voteDate, stbId, ptag });
  
    // some codes...
  }
  </code></pre>
  
  <p>where <code>VoteNumInfo</code> is a model for voting statistic, and <code>VoteInfo</code> is a model for voting record. </p>
  
  <p>When the Operation 2 gets an error, the Operation 1 can not roll back.</p>
  
  <p>Are there any solutions? Something like <code>@transaction</code> in Spring Framework?</p>
  
  <p>Please help me.</p>