Mongoose .insertMany 仅保存对象 ID 和版本
在使用 mongoose insertMany 填充集合时,我注意到它只保存数组中每个数据的对象 ID 和 版本,例如
但我的模型和数据的结构如下面的代码所示,
//SCHEMA MODEL
const { Schema, model } = require("mongoose");
const staffSchema = new Schema({
Phone: { String },
Timestamp: { String },
Fullname: { String },
});
module.exports = { staffConvert: model("staff", staffSchema) };
虽然我的实际数据来自将 excel 转换为 json,但这是数据的示例
//SAMPLE DATA ARRAY
const data = [
{
Phone: "92893249",
Timestamp: "8/24/2020 10:08:25",
Fullname: "Musa Yahaya",
},
{
Phone: "92893249",
Timestamp: "8/24/2020 10:08:25",
Fullname: "Musa Yahaya",
},
];
try {
if (data.length === 0) return console.log("No data to upload");
await staffConvert
.insertMany(data)
.then((docs) => {
console.log(
"success_msg",
"Staffs record uploaded successfully" + " " + docs
);
// req.flash("success_msg", "Staffs record uploaded successfully");
})
.catch((error) => {
console.log(
"Staffs record uploaded failed" + " " + error.message
);
});
} catch (error) {
console.log("Staffs record uploaded failed" + " " + error.message);
// req.flash(
// "error_msg",
// "Staffs record uploaded failed" + " " + error.message
// );
}
while using mongoose insertMany to populate a collection I noticed it's only saving the object id and version for each data in the array as such
but my model and data are structured as shown in code below
//SCHEMA MODEL
const { Schema, model } = require("mongoose");
const staffSchema = new Schema({
Phone: { String },
Timestamp: { String },
Fullname: { String },
});
module.exports = { staffConvert: model("staff", staffSchema) };
though my actual data is coming from converting excel to json but this a sample of the data
//SAMPLE DATA ARRAY
const data = [
{
Phone: "92893249",
Timestamp: "8/24/2020 10:08:25",
Fullname: "Musa Yahaya",
},
{
Phone: "92893249",
Timestamp: "8/24/2020 10:08:25",
Fullname: "Musa Yahaya",
},
];
try {
if (data.length === 0) return console.log("No data to upload");
await staffConvert
.insertMany(data)
.then((docs) => {
console.log(
"success_msg",
"Staffs record uploaded successfully" + " " + docs
);
// req.flash("success_msg", "Staffs record uploaded successfully");
})
.catch((error) => {
console.log(
"Staffs record uploaded failed" + " " + error.message
);
});
} catch (error) {
console.log("Staffs record uploaded failed" + " " + error.message);
// req.flash(
// "error_msg",
// "Staffs record uploaded failed" + " " + error.message
// );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须更改您的模型。您将属性定义为对象类型。如果你想这样定义它,你必须明确告诉 mongo 它是字符串类型而不是对象。
});
这应该可以解决你的问题
You have to make change to your model. You're defining your attribute as object type. If you want to define it like that, you have to explicitly tell mongo that it's string type not object.
});
This should solve your issue