能否请您告诉我为什么使用mongoos在MongoDB中插入许多无法正常工作的
这是我的代码 https://codesandbox.io/s/gallant-solomon-o91wp 它只插入一行,而不是多行
app.get("/saveData", async () => {
try {
const data = [
{
empid: "test123",
date: "19-Jul-2019"
},
{
empid: "test13",
date: "18-Jul-2019"
}
];
console.log("before save");
let saveBlog = await BlogPostModel.collection.insertMany(data, {
checkKeys: false
}); //when fail its goes to catch
console.log(saveBlog); //when success it print.
console.log("saveBlog save");
} catch (error) {
console.log(error);
}
});
答案 0 :(得分:0)
根据https://codesandbox.io/s/gallant-solomon-o91wp 这将不起作用,因为您的ID在架构中是唯一的,并且您尝试添加具有未定义ID的数据
// your data as per https://codesandbox.io/s/gallant-solomon-o91wp
const data = [
{
empid: "test123",
date: "19-Jul-2019"
},
{
empid: "test13",
date: "18-Jul-2019"
},
{
empid: "test13",
date: "11-Jul-2019"
}
];
解决方案 如下更改数据
const data = [
{
id: "1",
empid: "test123",
date: "19-Jul-2019"
},
{
id: "2"
empid: "test13",
date: "18-Jul-2019"
},
{
id: "3"
empid: "test13",
date: "11-Jul-2019"
}
];