Mongoose .insertMany 仅保存对象 ID 和版本

发布于 2025-01-10 03:53:28 字数 2154 浏览 0 评论 0原文

在使用 mongoose insertMany 填充集合时,我注意到它只保存数组中每个数据的对象 ID版本,例如

Mongo

但我的模型和数据的结构如下面的代码所示,

//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

Mongo

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花心好男孩 2025-01-17 03:53:28

您必须更改您的模型。您将属性定义为对象类型。如果你想这样定义它,你必须明确告诉 mongo 它是字符串类型而不是对象。

const staffSchema = new Schema({
Phone: String,
Timestamp: { type: String },

});

这应该可以解决你的问题

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.

const staffSchema = new Schema({
Phone: String,
Timestamp: { type: String },

});

This should solve your issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文