简体   繁体   中英

How to add an array of objects in Mongoose Schema

can someone help me?

I'm trying to update my Mongoose Schema and add to it an Array like this : I want to add to my ProductSchema the "attribute" ( I don't know how we call this, sorry ^^') description and in this description, you have multiple attributes lile brand, model, size, color, ...

I did something like this but (I don't think it worked) :

...
    description: [
        { brand: String },
        { model: String },
        { size: String },
        { color: String },
        { year: Number },
        { State: String }
    ]
...

Thank's for your time !

UPDATE:

In fact, I don't need an array for the description, that was a stupid question ^^'

I can do something like this :

...
    description: {
        brand: String,
        model: String,
        size: String,
        color: String,
        year: Number,
        State: String
    }
...

Sure, you don't need an array if there is only one "decription" in your document schema.

However, if you need to store multiple descriptions, you can use :

...
description: [{
    brand: String,
    model: String,
    size: String,
    color: String,
    year: Number,
    State: String
}]

In your first attempt, you've made each property of your object an object itself.

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