title: How to efficiently loop through a MongoDB collection in order update a sequence column?
body: <p>I am new to MongoDB/Mongoose and have a challenge I'm trying to solve in order to avoid a performance rabbit hole!</p>
  <p>I have a MongoDB collection containing a numeric column called 'sequence' and after inserting a new document, I need to cycle through the collection starting at the position of the inserted document and to increment the value of sequence by one. In this way I maintain a collection of documents numbered from 1 to n (i.e. where n = the number of documents in the collection), and can render the collection as a table in which newly inserted records appear in the right place.</p>
  <p>Clearly one way to do this is to loop through the collection, doing a seq++ in each iteration, and then using Model.updateOne() to apply the value of seq to sequence for each document in turn.</p>
  <p>My concern is that this involves calling updateOne() potentially <em>hundreds</em> of times, which might not be optimal for performance.  Any suggestions on how I should approach this in a more efficient way?</p>