mongodb - 使用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。请对此进行指导
解决方案
您收到的错误可能是您在更改后没有正确导入模型,您可以尝试此代码
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});
推荐阅读
- python - TypeError:'int' 对象在 Tkinker 电影预订 GUI 中不可下标
- python - 使用 PIL、plt、numpy 绘制直方图时出错
- swift - Swift JsonDecoder Networking Hi 未打印
- ruby-on-rails - 在 Rails 中使用局部变量(添加占位符)时出现问题?
- c# - 查找数组中的异常值?
- python - cross_validate 和样本数
- java - 错误:javaweb java.lang.NoClassDefFoundError:org/apache/commons/fileupload/FileItemFactory
- css - 单击时的 CSS 按钮
- sql - 如何在 Redshift ORDER BY 子句中首先按特定值排序?
- python - python - 我在哪里可以查找 types.FunctionType 的参数?