简体   繁体   中英

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

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

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

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. after this you will have to call the save() method.

example We have the passwordsArr part of the schema as an array of objects.

// passwordsArr modified

// mark the path as having changes to write to the DB user.markModified('passwordsArr');

// now saves the document 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: {
            type: Array,
            trim: true,
            required: true,
            maxlength: 3200
        }

Where the "type" is Array instead of String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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