title: How to loop for transaction in nodejs
body: <p>I'm trying to do the transaction in mongoodb using Fawn npm modules. 
  I have written the code below and it's working fine. But if item is an array has multiple elements, how can I perform the same action. In that case, I need to save order only one time but update the stock multiple time.</p>
  
  <p>I tried to loop over but the order is saved two times and there is an error which I'm getting is <code>MongoError: E11000 duplicate key error collection: db.orders index</code> It's because I'm saving same order multiple time.</p>
  
  <pre><code>try {
      new Fawn.Task()
        .save('orders', order)
        .update(
          'items',
          { _id: item[0]._id },
          { $inc: { stock: -+req.body.quantity[0] } }
        )
        .run();
    } catch (ex) {
      res.status(500).send('Something failed');
    }
  </code></pre>
  
  <p>where item is the list of all item's ID that I have to update.</p>
  
  <p>I want to update the order single time and other item multiple times based on the size of the item array.</p>
  
  <p>When I'm doing this, It works absolutely fine. 
  But I want it to be dynamic, as I won't be knowing the size of the array.</p>
  
  <p><a href="https://i.stack.imgur.com/X5NHS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X5NHS.png" alt="hard coded code"></a></p>