title: How to save form data as an objects and array of objects to mongodb using loop in node.js?
body: <p>I am new to MongoDB, I am using Node.js, Express 4 and mongoose(mongoDB) for my project.I stuck to save form data to mongoDB within loop and my model contains Objects and array of objects as well. </p>
  
  <p><strong>Model</strong> :  </p>
  
  <pre><code>var Subscriber = new Schema({  
      first_name: String,  
      emergency_contact_1: {  
          name: String,  
          number: [{  
              type: String  
          }]  
      },  
      residential: {  
          phone: String,  
          address: String,  
          ...  
      },  
      medications: [  
      {  
          visit_id: { type: Object },  
          name: String,  
          ....  
      }],  
      food_allergies: [  
          {type: String}  
      ],  
      ....  
  });  
  </code></pre>
  
  <p><strong>Controller</strong> :<br>
  I want to save data in this way:</p>
  
  <pre><code>var subscriber = new Subscriber();
  
  //Here I am trying to save all form's fields to mongoBD fields.
  for (var field in form_data) {
      subscriber[field] = form_data[field];
  }
  
  subscriber.save(function (err1, instance) {
      if (err) {
          console.log("error");
          return res.send("...");
      }
      console.log("saved successfully");
  }
  </code></pre>
  
  <p>Normal fields are getting saved properly using above loop but when objects or arrays came then it won't get save to mongoDB.  </p>
  
  <p>Any solution ? or any other way to insert/save data through loop to mongoDB model ?<br>
  Any help would be appreciated.Thank you..!!</p>