title: Mongodb inserting element into array using $position
body: <p>I am connecting to mongodb version 3.6.3 using node v9.10.1. I am inserting an element into an existing record with an array using the $position as i need to keep the array sorted based on the position. The elements arrive randomly on the server. My problem is even after using the $position, the elements are not inserted at the said position
  My document structure is as follows</p>
  
  <pre><code>{_id:1,array;[]}
  </code></pre>
  
  <p>The code to test the same in node is as follows</p>
  
  <pre><code>let mongo = require('mongodb')
  let client = mongo.MongoClient;
  let pos = [0, 6, 3, 4, 5, 1, 2];
  client.connect("mongodb://localhost/test", function (err, mdb) {
      if (!err) {
          let collection = mdb.collection('docs');
          for (let i = 0; i &lt; 7; i++) {
              let position = pos[i];
              console.log("newindex ", newindex, " i ", i);
              let element = {
                  "id": i,
                  "createdOn": new Date(),
                  "content": i,
                  "pos": position
              };
              let doc = { '$push': { 'array': { '$each': [element], '$position': position } } };
              collection.update({ "_id": 1 }, doc, function (err, obj) {
              })
          }
      }
  })
  </code></pre>
  
  <p>After running this you would find the insertions of the element into array is not at the desired positions.
  Is it a mongo error or mine?</p>