I'm using MongoDB (v3.4) as a cache and using TTL indexes to expire records. However, the TTL settings don't seem to work properly. Specifically, I tested using an endpoint to insert data (as below).
The endpoint mongoInsert is supposed to expire in 5 min. However, it seems that after ~1 min, the document got removed. I've looked up other similar suggestions regarding the use of UTC time using moment().utc().toDate(), and the behaviour was the same. new Date() returned UTC time so I guess it should be the same effect.
Not sure if there are other settings that should be included but are not detailed in the documentation. Anyone encountered this before?
function mongoInsert(mongoClient){
return function(req, res){
mongoClient.connect('mongodb://localhost:27017/cache', function(err, db) {
db.collection('data', function(err, collection){
var testItems = [{
"_id": "abc12345",
"dmac": "abc",
"createdAt": new Date(),
"val": {
"0": 10,
"1": 15,
"2": 5
},
"res": "minutes",
"time": "2017-12-04T00:12:58Z"
}];
let unix = new moment().valueOf();
collection.createIndex({createdAt: unix}, {expireAfterSeconds: 300});
collection.insertMany(testItems, function(err, items){
db.close();
res.json(items);
});
})
})
}
}
collection.createIndex({createdAt: 1}, {expireAfterSeconds: 60,unique:true});
and
collection.createIndex({createdAt: 1}, {expireAfterSeconds: 300,unique:true});
this is invalid
You cannot use createIndex() to change the value of expireAfterSeconds of an existing index. Instead use the collMod database command in conjunction with the index collection flag. Otherwise, to change the value of the option of an existing index, you must drop the index first and recreate.
https://docs.mongodb.com/v3.4/core/index-ttl/
For expiry of individual documents, there are reports that it can only be done by calculating the expiry time and expiring them by a specific clock time (ref: groups.google.com/forum/#!topic/mongodb-dev/ZLb8KSrLyOo).
var testItems = [{
"_id": "abc12345",
"dmac": "abc",
"expireAt": moment().add(3, 'minutes').toDate(),
"val": {
"0": 10,
"1": 15,
"2": 5
},
"res": "minutes",
"time": "2017-12-04T00:12:58Z"
}];
let unix = new moment().valueOf();
collection.createIndex({expireAt: unix}, {expireAfterSeconds: 0}, function(error, indexName){
if(error) console.log(error);
console.log(indexName);
});