title: How to add field to Mongoose Schema via route
body: <p>I'm using mongodb and mongoose to manage database in my application. As backend I use NodeJS. In my case I have to collect data from some devices. The no of devices are dynamically changing. How can I add schema field via route. My schema is,</p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const dataStructure = mongoose.Schema({
  data: [{
      Node_1: [{
          sensor_1: { type: Number },
          sensor_2: { type: Number },
          sensor_3: { type: Number },
          sensor_4: { type: Number }
          }]
      }]
  })
  
  module.exports = mongoose.model('Data', dataStructure);
  </code></pre>
  
  <p>As in this schema, I have <code>data</code> array to store data. If I have 2 Nodes, I want do add <code>Node_2</code> to my schema to log data from both 2 nodes. In some cases I want to log data from 3 different Nodes, like that I need to change schema from <strong>client side</strong>. I need to do this from client side, </p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const dataStructure = mongoose.Schema({
  data: [{
      Node_1: [{
          sensor_1: { type: Number },
          sensor_2: { type: Number },
          sensor_3: { type: Number },
          sensor_4: { type: Number }
          }],
      Node_2: [{
          sensor_1: { type: Number },
          sensor_2: { type: Number },
          sensor_3: { type: Number },
          sensor_4: { type: Number }
      }]
  })
  
  module.exports = mongoose.model('Data', dataStructure);
  </code></pre>
  
  <p>How can I do it.</p>
  
  <p>I tried this route,</p>
  
  <pre><code>const dynamic_schemas = require('mongoose-dynamic-schemas');
  
  const Data = require('../schema/dataschema');
  
  router.post('/addNode', (req, res, next) =&gt; {
      const nodeNumber = req.body.nodeNumber;
      Data
          .then(result =&gt; {
          dynamic_schemas.addSchemaField(Data.data, "Node_"+nodeNumber, 
                  [{ 
                  sensor_1: Number, sensor_2: Number, sensor_3: Number, 
                  sensor_4: Number 
                  }] )
          })
          .catch(err =&gt; {
              if(err){
                  res.status(500).json({
                      state: false
                  })
              } else{
                  res.status(200).json({
                      state: true
                  })
              }
      })
   })
  </code></pre>
  
  <p>I'm getting error that saying <code>Cannot overwrite 'Data' model once compiled.</code></p>
  
  <p>Can anyone help me.</p>