简体   繁体   English

如何在 mongoose 中保存对象数组?

[英]How to save array of objects in mongoose?

Hi so i have an array of objects that look like this嗨所以我有一个看起来像这样的对象数组

[{
             "id": 0,
             "name": "Del Vecchios | Italian Food | Asheville, NC",
             "domain": "delvecchiositalian.com",
             "email": "eat@delvecchiositalian.com",
             "phone": "828-258-7222",
            
       },
       {
             "id": 1,
             "name": "DeRango's Pizza Palace | Italian Restaurant | Racine, WI",
             "domain": "derangos.com",
             "email": "info@derangospizzapalace.com",
             "phone": "262-639-4112",
             
       },
       {
             "id": 2,
             "name": "Michigan's Premier Flooring Installation Services | D.E. McNabb",
             "domain": "demcnabb.com",
             "email": "no email",
             "phone": "(248) 587-1500",
             
       },
}]

And i want to store it in my mongo database but i dont know how to make the schema, my actual schema looks like this我想将它存储在我的 mongo 数据库中,但我不知道如何制作架构,我的实际架构看起来像这样

const mongoose = require("mongoose");

const infoSchema = new mongoose.Schema(
    {
        info: {
            type: String,
            trim: true,
            required: true,
            maxlength: 3200
        }
    },
    { timestamps: true }
);

module.exports = mongoose.model("ScrapedInfo", infoSchema);

and this is the controller for saving the data这是用于保存数据的 controller

router.post('/stored', (req, res) => {
        const info = new ScrapedInfo(req.body)
        info.save((err) => {
                if (err) {
                    console.log(err+"error")
                }
                else{
                        console.log('saved')
                }
            });   
});

Dunno if i am making a mistake in the controller, seems fine to me, but i every time i run the button for the controller, i have the rror ValidationError: info Path info is required不知道如果我在 controller 中犯了一个错误,对我来说似乎很好,但我每次运行 controller 的按钮时,我都有错误 ValidationError: info Path info is required

When trying to update an array of objects, using mongoose, you will have to tell MongoDB that there are pending changes by using the markModified(path) method.当尝试使用 mongoose 更新对象数组时,您必须使用 markModified(path) 方法告诉 MongoDB 有待处理的更改。 after this you will have to call the save() method.在此之后,您将不得不调用 save() 方法。

example We have the passwordsArr part of the schema as an array of objects.示例 我们将模式的 passwordsArr 部分作为对象数组。

// passwordsArr modified // passwordsArr 已修改

// mark the path as having changes to write to the DB user.markModified('passwordsArr'); // 将路径标记为有更改以写入数据库 user.markModified('passwordsArr');

// now saves the document user.save(function (err, user) { if (err) return next(err); // 现在保存文档 user.save(function (err, user) { if (err) return next(err);

Your schema should look something like this.您的架构应该看起来像这样。 The "Path info is required error" is caused by the fact that the key "info" doesn't exist in the Schema. “路径信息是必需的错误”是由于模式中不存在密钥“信息”引起的。

    const mongoose = require('mongoose')
    const Schema = mongoose.Schema
    
    // Schema
    const infoSchema = new Schema(
        {
            name: {
                type: String,
                required: true,
            },
            domain: {
                type: String,
                required: true
            },
            email: {
                type: String,
                required: true
            },
            phone: {
                type: String,
                required: true
            }
    },
    { timestamps: true }
)

// Model
module.exports = mongoose.model('Info',infoSchema)

If you want to save an array your "info" key should look like this:如果你想保存一个数组,你的“info”键应该是这样的:

info: {
            type: Array,
            trim: true,
            required: true,
            maxlength: 3200
        }

Where the "type" is Array instead of String.其中“类型”是数组而不是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM