How to change order of array with MongoDB?
<p>I need to be able to increment and decrement the position of an element of an array in a <code>MongoDB</code> object.</p>

<p>I looked at the <code>&lt;update&gt;</code> API in the <a href="https://docs.mongodb.org/manual/reference/operator/update/" rel="noreferrer">MongoDB API</a> but could not find anything to let me do so.</p>

<p>I am trying to use <code>findOneAndUpdate</code> through <code>Mongoose</code> and I know the index of the element I am trying to shift up or down.</p>

<p>An example of the array item of <code>base64</code> encoded images:</p>

<pre><code>{ 
  images: [
    "img1",
    "img2",
    "img3"
  ]
}
</code></pre>

<p>And I would like to move, for example "img2", up or down (but "image" should not be able to pushed up since there is nowhere to go).</p>

<p>If I wanted to push "img2" up then the result would be:</p>

<pre><code>{ 
  images: [
    "img2",
    "img1",
    "img3"
  ]
}
</code></pre>

<p>It doesn't matter if I achieve this through changing the index, swapping, or pushing up/down.</p>