1

I have to implement mongodb ttl to remove document from the collections. I have set 60 sec for a document to expire but it does'nt get remove. The document is not getting removed:

MongoClient.connect(url,function(err,db){
        if(err) throw err;
        var dbo = db.db("mydb");
        dbo.collection("users").drop();
        dbo.collection("users").createIndex({"created_at": 1},{expireAfterSeconds: 60});
        dbo.collection("users").insert({name: "why","created_at" : new Date()});
    })
> db.users.find()
{ "_id" : ObjectId("5ca042066a4e9f23f0324196"), "name" : "why", "created_at" : ISODate("2019-03-31T04:28:54.128Z") }

Query of getIndexes:  
> db.users.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "mydb.users"
        },
        {
                "v" : 2,
                "key" : {
                        "created_at" : 1
                },
                "name" : "created_at_1",
                "ns" : "mydb.users",
                "expireAfterSeconds" : 60
        }
]
Zing
  • 23
  • 6
  • TTL runs every minute ( approximately ), so 60 seconds is too small for a reliable interval. – Neil Lunn Mar 31 '19 at 04:46
  • @NeilLunn I've tried all the previous posts on this topic . But it did'nt work out . – Zing Mar 31 '19 at 05:24
  • If you are still trying to do 60 seconds after reading all that then I'm not surprised. The lesson here is the interval is too short. – Neil Lunn Mar 31 '19 at 05:28
  • @NeilLunn What should be the ideal interval ? I mean , i need to remove the document in the provided time , what should i do ? – Zing Mar 31 '19 at 05:38

0 Answers0