我必须实现mongodb ttl才能从集合中删除文档。 我已将文档设置为60秒过期,但不会删除。 该文档没有被删除:
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
}
]