0

I want to delete a particular item from the mongo collection after a given time. I tried expireAfterSeconds using mongoose schema, expires in index of schema and different methods. Even after 60 seconds also when I refresh my mongo database the item in collection is present and after 10 mins also it is there and not getting delted automatically

I tried in two different ways

timeCreated: { type: Date, default: Date.now, expires: 20 },

admin: { type: Boolean, default: false, index : {expires: '10s'} },

  • Can you post the full schema code to show how you're using the schema constructor? – Len Joseph Jul 09 '19 at 13:24
  • var user = new mongoose.Schema{ user_deleted: { type: Boolean, default: false }, token: { type: [String], index : {expires: '10s'} }} – Raghu Sreeram Jul 09 '19 at 13:28
  • This problem seems more complex than I expected, since you are trying to expire an item and not a whole collection. I think this article will help: https://dojotipsntricks.wordpress.com/2014/09/10/custom-expiry-times-for-mongoose-documents-in-node-js/ – Len Joseph Jul 09 '19 at 13:36
  • I tried this also but of no use – Raghu Sreeram Jul 09 '19 at 13:40
  • I looked at this article: https://medium.com/@Baresse/auto-expire-documents-with-mongodb-772fee84cc23 which describes the underlying method using the MongoDB driver. It looks like an option without using Mongoose is to use the native driver to create an expiry index explicitly. – Len Joseph Jul 09 '19 at 13:49
  • for doing this I tried db.users.createIndex( { "token":1 }, { expireAfterSeconds: 10 } ) in linux terminal but after sometime I used db.users.find() to get all the items of collection after 10 mins the also in terminal it is showing there. , – Raghu Sreeram Jul 09 '19 at 13:53

0 Answers0