Search code examples
node.jsmongodbmongoosemongoose-schema

Mongodb erase documents which have expired (mongoose)


I have a schema the following schema

const exampleSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    required: true,
    min: Date.now(),
  },
});

I want the documents which have expired based on the field date of this schema to be deleted at the end of the day. For example, let's say I insert a document with the date field: 2021-07-05 10:00 am, I want this document to be deleted at the end of that day.


Solution

  • Expire data from collection by setting Time to live.