Implementing Soft Delete in Mongoose

Aug 7, 2022

A soft delete means setting an isDeleted flag on the document to mark a document as deleted, rather than actually deleting the document. This means you can keep the document for future analysis.

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String,
  isDeleted: Boolean
});

Using Query Middleware

You can use middleware to filter out documents that have isDeleted: true, so Mongoose only returns documents that don't have isDeleted: true.

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String,
  isDeleted: { type: Boolean, defaults: false }
});

testSchema.pre('find', function() {
  this.where({ isDeleted: false });
});

testSchema.pre('findOne', function() {
  this.where({ isDeleted: false });
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({
    name: 'Test'
  });
  await Test.create({
    name: 'HiddenTest',
    isDeleted: true
  });

  // Only returns `Test` document, not `HiddenTest`
  const docs = await Test.find();
}

run();

Want to become your team's MongoDB expert? "Mastering Mongoose" distills 8 years of hard-earned lessons building Mongoose apps at scale into 153 pages. That means you can learn what you need to know to build production-ready full-stack apps with Node.js and MongoDB in a few days. Get your copy!

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Mongoose Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!