How to delete data from table from node js in mongoose?

Here, we will write a program to delete data from a table in mongoose using node js. First we need to make sure that we have created a successful connection with mongoose and it has a database with a table in place.

app.js file

// Import Mongoose
const mongoose = require('mongoose');

// Import the User model
const User = require('./models/user');

// Connect to the database
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true })
  .then(() => console.log('Connected to database'))
  .catch(err => console.log(err));

// Define the filter
const filter = { email: 'abc@gmail.com' };

// Delete the document from the database
User.deleteOne(filter)
  .then(result => console.log(result))
  .catch(err => console.log(err));