this is my schema
const mongoose = require('mongoose')
const MaterialListSchema = new mongoose.Schema({
nomenclature:{
type: String,
required: true
},
national: {
type: String,
required: true
},
partnumber:{
type:String,
required:true
},
quantity: {
type: Number,
required: true
},
unit:{
type: String,
required: true
},
price: {
type: Number,
required: true
}
})
const MrrlSchema = new mongoose.Schema({
aircrafttype:{
type: String,
required: true
},
mrrlcategory:{
type: String,
required: true
},
materiallist: [MaterialListSchema]
})
const Mrrl = mongoose.model('Mrrl', MrrlSchema)
module.exports = Mrrl
this is my update code . but it will delete all the sub document on the selected and will only have remaining
Mrrl.updateOne({
'materiallist': {$elemMatch: { _id: req.params.id}}
},{
$set: { materiallist: req.body }
}).then((data)=>{
console.log(data)
})
1. Do find 'materiallist': {$elemMatch: { _id: req.params.id}} 2. Do map ( in nodejs ) 3. And do set with new array
It is not so dramatic for small data – Pavlo Kostohrys Sep 24 '20 at 13:34