首页 > 解决方案 > 使用express在mongodb中插入多条记录

问题描述

let countries = [ {
{
      code: "1",
      sortname: "US",
      name: "USA"
    },
{
}...
} ]

countries.forEach(country => {
    let mycountry = new Countries({
      mycountry.code : country.code,
      mycountry.name : country.name,
      mycountry.sortname : country.sortname
    })
    await mycountry.save();
  })
  res.json({success: true});

如何使用 express 将多条记录插入 mongodb。请对此进行指导

标签: mongodbexpress

解决方案


您收到的错误可能是您在更改后没有正确导入模型,您可以尝试此代码

let mycountry = '';
  countries.forEach(country => {
      mycountry = new Countries({
      code : country.code,
      name : country.name,
      country_id : country.country_id
    })   
    mycountry.save();
     
  })
  
 res.json({success: true});

推荐阅读