0

Though I set expiration time , but still, data was not deleted from MongoDB .How can I solve this ? or what is wrong with my code .....?

my mongoose version 6.1.2

const mongoose = require('mongoose')

const AccessTokenSchema = mongoose.Schema({
    email:{
        type: String,
        required: true
    },
    token:{
        type: String,
        required: true,
    },
    createdAt: {
        type: Date,
        default: Date.now,
        index: { expires: '3m' }
    }
},{
    versionKey: false,
})

const AccessTokenModel = mongoose.model('AccessToken',AccessTokenSchema)

module.exports = AccessTokenModel

Data in mongodb

  • Does this answer your question? [Setting expiry time for a collection in mongodb using mongoose](https://stackoverflow.com/questions/14597241/setting-expiry-time-for-a-collection-in-mongodb-using-mongoose) – Tyler2P Dec 21 '21 at 10:46
  • The problem is same the but that solution not working for me... – Ataur Rahman Dec 21 '21 at 10:50
  • Does the createdAt field actually have a value for all documents? Did you verify the index was created? – Joe Dec 21 '21 at 10:55
  • @Joe yes. in createAt field have a value of creating each date creating time . here is a database screenshot . **[schreenshort](https://i.stack.imgur.com/BYI76.png)** – Ataur Rahman Dec 21 '21 at 11:00
  • the version of your mongodb? – Marco Bertelli Dec 21 '21 at 15:42
  • @MarcoBertelli , I use mongoose here...behind the seen mongoose use MongoDB , but I think that's why no need to install MongoDB directly.Is there any need to install MongoDB ? – Ataur Rahman Dec 21 '21 at 20:31
  • sure, mongoose have a basic deps that automatically install mongoDB – Marco Bertelli Dec 22 '21 at 08:42
  • but into the db what is the version? – Marco Bertelli Dec 22 '21 at 08:43
  • @MarcoBertelli , sorry to say that , in my package.json I am not found the MongoDB version. here is my package.json file [screenshort](https://prnt.sc/245fzif) . I am connected to MongoDB with mongoURI with mongoose . And my connected MongoDB version is 4.4.10 . But I am not install MongoDB in my project . – Ataur Rahman Dec 23 '21 at 09:42
  • Steps to troubleshoot: connect directly to the mongod with compass or a shell, make sure an index exists with the expireAfterSeconds option, then make sure the TTL monitor is enabled, and has a reasonable interval set. – Joe Dec 31 '21 at 20:59

0 Answers0