mongoose delete code example
Example 1: mongoose delete property
User.collection.update({_id: user._id}, {$unset: {field: 1 }});
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: deleteOne mongoose
Tank.deleteOne({ size: 'large' }, function (err) {
if (err) return handleError(err);
});
Example 4: mongoose model
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = mongoose.model('Tank', schema);
Example 5: create document mongoose
var Tank = mongoose.model('Tank', yourSchema);
var small = new Tank({ size: 'small' });
small.save(function (err) {
if (err) return handleError(err);
});
Tank.create({ size: 'small' }, function (err, small) {
if (err) return handleError(err);
});
Tank.insertMany([{ size: 'small' }], function(err) {
});
Example 6: delete document mongose
Model.deleteOne({ options }, function (err) {});