Mongoose在一条记录中保存了许多ID

时间:2018-01-13 02:34:32

标签: node.js mongodb mongoose

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')

 })
}

这是我的代码,但是当我运行它时,我得不到我期望得到的

{ 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 }] 
   }

我希望在account_charges中没有_id。请有人帮助我找出为什么在帐户费用中打印_id或我做错了什么

1 个答案:

答案 0 :(得分:0)

此处account_charges是AccountType中的子文档数组。 对于每个子目录,Mongoose将插入一个自动生成的__id,这对于查询parent.children.id(_id);

非常有用

在定义架构时,可以使用{_id:false}关闭将__id添加到subdocs的这种行为。

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