this is my code to update the doc
UserAddress.findOneAndUpdate({ "_id": _userAddress._id, "address._id": address._id }, {
"$set": {
"address.$": {
"name": address.name,
"mobileNo": address.mobileNo,
"pincode": address.pincode,
"landmark": address.landmark,
"address": address.address,
"area": address.area,
"state": address.state,
"alternatePhoneNo": address.alternatePhoneNo,
"addressType": address.addressType
}
}
})
.exec((error, data) => {
if(error) return res.status(400).json({ error })
if(data) return res.status(201).json({ data })
console.log(error)
console.log(data)
})
//Here is my Schema
const addressSchema = mongoose.Schema({
name: {
type: String,
required: true,
trim: true,
min: 2,
max: 40
},
mobileNo: {
type: String,
required: true,
trim: true
},
pincode: {
type: String,
required: true,
trim: true
},
landmark: {
type: String,
required: true,
trim: true
},
address: {
type: String,
required: true,
trim: true
},
area: {
type: String,
required: true,
trim: true
},
state: {
type: String,
required: true,
trim: true
},
alternatePhoneNo: {
type: String
},
addressType: {
type: String,
required: true,
enum: [ 'home', 'work' ]
}
})
const userAddressSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
address: [addressSchema]
}, { timestamps: true })