shopSchema
const shopSchema = new Schema({
shopName: {
type: String,
required: true
},
foodId: [{
type: Schema.Types.ObjectId,
ref: 'Food',
}]
});
foodSchema
const foodSchema = new Schema({
foodName: {
type: String,
required: true
},
image: {
type: String,
required: true
},
price: {
type: String,
required: true
},
shopName: {
type: String,
required: true
},
shopId:{
type: Schema.Types.ObjectId,
ref: 'Shop',
required: true
}
});
In this if i delete the food from foodschema how to delete the foodid referenced in shopschema
exports.postdelfood=(req,res,next)=>{
const fid=req.params.foodId;
console.log(fid);
return Food.deleteOne({_id:fid})
.then(result=>{
console.log('deleted');
res.status(200).json({message:'success'});
})
.catch(err=>{
res.status(500).json({message:'failed'});
})
};
is there any function in mongoose to delete all the reference id if we delete anyone of the id?