title: Updating Nested Array Mongoose
body: <p>I am working on an express js application where I need to update a nested array. 
  1) Schema :</p>
  
  <pre><code>//Creating a mongoose schema
  var userSchema = mongoose.Schema({
  _id: {type: String, required:true},
  name: String,
  sensors: [{
      sensor_name: {type: String, required:true},
      measurements: [{time: String}]
  }] });
  </code></pre>
  
  <p>2) 
  Here is the code snippet and explanation is below:</p>
  
  <pre><code>router.route('/sensors_update/:_id/:sensor_name/')
  .post(function (req, res) {
  User.findOneAndUpdate({_id:req.body._id}, {$push: {"sensors" : 
  {"sensor_name" : req.body.sensor_name , "measurements.0.time": req.body.time } } },
  {new:true},function(err, newSensor) {
  if (err)
  res.send(err);
  res.send(newSensor)
  }); });
  </code></pre>
  
  <p>I am able to successfully update a value to the measurements array using the findOneAndUpdate with push technique but I'm failing when I try to add multiple measurements to the sensors array. </p>
  
  <p>Here is current json I get if I get when I post a second measurement to the sensors array :</p>
  
  <pre><code>{
  "_id": "Manasa",
  "name": "Manasa Sub",
  "__v": 0,
  "sensors": [
  {
    "sensor_name": "ras",
    "_id": "57da0a4bf3884d1fb2234c74",
    "measurements": [
      {
        "time": "8:00"
      }
    ]
  },
  {
    "sensor_name": "ras",
    "_id": "57da0a68f3884d1fb2234c75",
    "measurements": [
      {
        "time": "9:00"
      }
    ]
    }]} 
  </code></pre>
  
  <p>But the right format I want is posting multiple measurements with the sensors array like this :</p>
  
  <p><a href="https://i.stack.imgur.com/T3wgB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/T3wgB.png" alt="enter image description here"></a></p>
  
  <p>Right JSON format would be :</p>
  
  <pre><code> {
  "_id" : "Manasa",
  "name" : "Manasa Sub",
  "sensors" : [ 
      {
          "sensor_name" : "ras",
          "_id" : ObjectId("57da0a4bf3884d1fb2234c74"),
          "measurements" : [ 
              {
                  "time" : "8:00"
              }
          ],
          "measurements" : [ 
              {
                  "time" : "9:00"
              }
          ]
      }],
  "__v" : 0 }
  </code></pre>
  
  <p>Please suggest some ideas regarding this. Thanks in advance.</p>