Mongoose TTL expiring even when condition is false

  Kiến thức lập trình

I have this mongoose Schema, I would like to the model delete itself after 60 seconds if the field paid is false.
However when using TTL the document expires even if the field paid is true.
I’ve even tried to create a TTL inside the schema but with no success. What should I do?

const mongoose = require('mongoose');

const donationSchema = new mongoose.Schema({
    user: { type: mongoose.SchemaTypes.ObjectId, ref: 'User' },
    ngo: { type: mongoose.SchemaTypes.ObjectId, ref: 'Ngo', required: true },
    type: { type: String, default: 'ads', required: true },
    amount: { type: Number },
    iat: { type: Date, default: Date.now },
    paid: {
        type: Boolean,
        default: false,
    },
    orderID: { type: String },
});

donationSchema.index(
    { createdAt: 1 },
    {
        expireAfterSeconds: 60,
        partialFilterExpression: { paid: false },
    },
);

function autoPopulateChildren(next) {
    this.populate('user', 'fullName level avatar country -_id');
    next();
}

donationSchema.pre('find', autoPopulateChildren);

module.exports = new mongoose.model('Donation', donationSchema);

LEAVE A COMMENT