mongoose findbyid and delete code example
Example 1: findbyid and delete mongoose await
Subscriber.findByIdAndRemove(subscriberId)
.then(() => {
res.locals.redirect = "/subscribers";
next();
})
.catch(error => {
console.log(`Error deleting subscriber by ID: ${error.message}`);
next();
});
Example 2: mongoose find by and delete
Campground.findByIdAndRemove(req.params.id, function(err){
if(err){
res.redirect("/campgrounds");
} else {
res.redirect("/campgrounds");
}
});
Example 3: mongoose model
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = mongoose.model('Tank', schema);
Example 4: delete document mongose
Model.deleteOne({ options }, function (err) {});
Example 5: model mongoose
const modelName = mongoose.model("collectionname", collectionSchema);
const fruitSchma = new mongoose.Schema ({
name: String
});
const Fruit = mongoose.model("Fruit", fruitSchema);