0
exports.save = (req, res)=>{

  console.log(req.body)

  let account_type_data = new AccountType()
  account_type_data.account_name  = req.body.account_name
  account_type_data.account_type  = req.body.account_type
  account_type_data.account_bin   = req.body.account_bin
  account_type_data.account_charges = []

  let charge_name = req.body.account_charge_name

  for(var x = 0; x < charge_name.length; x++){
     let push_value = {
         charge : req.body.account_charge_value[x],
         name : req.body.account_charge_name[x],
         order : req.body.account_charge_order[x],
         reoccurence : req.body.account_charge_reoccurence[x]
       }
    account_type_data.account_charges.push(push_value)
   }

   console.log(account_type_data)

 account_type_data.save((err)=>{
     if(err) return res.render('settings/all', { validated : req.body, csrfToken: req.csrfToken(), error : err})

  res.render('settings/all')

 })
}

This is my code but when i run it I dont get what I expect to get

{ account_bin: '50',
account_type: '1D',
  account_name: 'Daily contribution regular',
  _id: 5a596d43db4788ddb296e41a,
  status: 0,
  account_charges:
   [ { name: 'Opening',
       order: 0,
       reoccurence: true,
       _id: 5a596d43db4788ddb296e41b,
       charge: 1000 }] 
   }

I expect to get without the _id in the account_charges. Please can someone help me to find out why an _id is printed in the account_charges or what i am doing wrong

Schnecke
  • 502
  • 1
  • 6
  • 18

1 Answers1

0

Here account_charges is an array of subdocuments in AccountType. For every subdoc Mongoose will insert an auto generated __id which can be useful for querying like parent.children.id(_id);

This behaviour of adding __id to subdocs can be turned off using { _id : false } when defining schema.

Stop Mongoose from creating _id property for sub-document array items

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42