当前位置: 代码迷 >> 综合 >> Mongoose - 一次录入多条数据insertMany
  详细解决方案

Mongoose - 一次录入多条数据insertMany

热度:16   发布时间:2024-01-15 11:44:53.0

使用 insertMany

// 输入数据
{"list":[{"areaOfPharmacistBackup":"湖北省武汉市黄陂区","nameOfPharmacistBackup":"鲁**","typeOfPharmacistBackup":"中药学","scopeOfPharmacistBackup":"零售","unitOfPharmacistBackup":"XXX公司1","numberOfPharmacistBackup":"422212341234","validTimeOfPharmacistBackup":"2021-11-22"},{"areaOfPharmacistBackup":"陕西省西安市未央区","nameOfPharmacistBackup":"杨*","typeOfPharmacistBackup":"药学","scopeOfPharmacistBackup":"零售","unitOfPharmacistBackup":"XXX公司2","numberOfPharmacistBackup":"611212341234","validTimeOfPharmacistBackup":"2021-12-20"}]
}// 代码
let pharmacistBackups = req.body.list;
if (typeof pharmacistBackups === 'string') {pharmacistBackups = JSON.parse(pharmacistBackups);
}
PharmacistBackup.remove().then(() => PharmacistBackup.insertMany(pharmacistBackups)).then(() => res.send({code: '400000'})).catch(err => next(err));