Home > Software engineering >  How to insert array of object in mongoose ExpressJs
How to insert array of object in mongoose ExpressJs

Time:09-19

I want to insert any product that will have some info and different size with price. I can insert all other data but array of object(size with price) is not storing in mongo.

Here is my Product Schema:

const productSchema = new mongoose.Schema({
    product_name: {type:String, required: true},
    product_desc: {type: String, required:true},
    prod_cat: {type: mongoose.Schema.Types.ObjectId,ref: 'Category'},
    prod_img: {type: String},
    pro_price: [{
        prod_size: {type: String,},
        prod_price: {type: String}
    }]
},{timestamps: true})
const prodSchema = mongoose.model('Product',productSchema)

this is my controller to post that data:

 async postProduct(req,res){
            try{
                
                const newProduct = new prodSchema({
                    product_name: req.body.productName,
                    product_desc: req.body.productDetails,
                    prod_cat: req.body.category,
                    pro_price: req.body.pro
                })
                
                console.log(newProduct);
                const addProduct = newProduct.save()
                
            }
            catch(err){
                console.log(err);
            }
        }

I have sent data from a form and the object looks like this:

{
  productName: 'Margerita',
  productDetails: 'Served with onion, garlic sauce, meat slice, white sauce',
  category: '63221514e6b0279b001b9187',
  pro: [
    { size: 'Medium', price: '750' },
    { size: 'Big', price: '900' },
    { size: 'Small', price: '500' }
  ]
}

The data is storing in DB like this::( here note that all other data is storing except the array of object i've passed ) . Seems like i have messed while mapping (suggest me to map my re.body.pro array)..

MongoDb stored that object liek this:

{"_id":{"$oid":"632372b5c1d5d998a2db49ae"},"product_name":"Margerita","product_desc":"Served with onion, garlic sauce, meat slice, white sauce","prod_cat":{"$oid":"63221619fc117e0cee38d855"},"pro_price":[{"_id":{"$oid":"632372b5c1d5d998a2db49af"}},{"_id":{"$oid":"632372b5c1d5d998a2db49b0"}},{"_id":{"$oid":"632372b5c1d5d998a2db49b1"}}],"createdAt":{"$date":{"$numberLong":"1663267509614"}},"updatedAt":{"$date":{"$numberLong":"1663267509614"}},"__v":{"$numberInt":"0"}}

CodePudding user response:

Most likely because your price schema (pro_price) expects prod_size and prod_price but the data in your example uses size and price. Try changing schema or data to use the same name.

For example, you could send the following data

{
  productName: 'Margerita',
  productDetails: 'Served with onion, garlic sauce, meat slice, white sauce',
  category: '63221514e6b0279b001b9187',
  pro: [
    { prod_size: 'Medium', prod_price: '750' },
    { prod_size: 'Big', prod_price: '900' },
    { prod_size: 'Small', prod_price: '500' }
  ]
}
  • Related