有没有办法将 MongoDb 中的集合配置为自动从 MeteorJS 过期?我已经从 Mongo site 看到了如何做到这一点,但不确定如何从 Meteor 执行此操作:
Tasks.insert({
text: text,
createdAt: new Date(),
});
//None of these work:
Tasks.ensureIndex( { "createdAt": 1 }, { expireAfterSeconds: 2 } );
Tasks._ensureIndex( { "createdAt": 1 }, { expireAfterSeconds: 2 } );
Tasks.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 2 } );
最佳答案
为什么不自己删除呢? insert
的第二个参数是一个回调,里面可以使用setTimeout
等待2秒:
Tasks.insert({
text: text,
createdAt: new Date(),
}, function(err, _id) {
if (_id != null) {
Meteor.setTimeout(function() {
Tasks.remove(_id);
}, 2000); // 2 seconds delay
}
});
关于javascript - meteor JS : How to expire mongo data automatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922148/