这是我的代码...请审阅它。这会自动删除记录,而不提及秒数。我也将创建索引更改为确保索引...但仍然不起作用
var mongo = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/testing5";
var extend;
var time = "3600";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbase = db.db("testing5");
var myobj = ({
"name": "Adnan khan",
"email": "xyz@gmail.com",
"createdAt": new Date(),
});
dbase.collection("log_events").createIndex({ "createdAt": 1 }, { expireAfterSeconds: time })
dbase.collection("log_events").insertOne(myobj, function(err, result) {
if (err) throw err;
resultant = result
console.log("data inserted and will be deleted approximately after 20 seconds");
db.close();
});
});
您需要
将 ttl 值指定为数字。所以不是写
var time = "3600";
尝试写作
var time = 3600;
另外,请注意,这是 3600 秒,所以 1 小时而不是 20 分钟......
https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds