MongoDB expireAfterSeconds不会删除文档
我希望MongoDB在通过%秒后清除其集合中的数据。 我正在设置索引,但收集不会在一段时间后被清除,所有的文件仍然存在。
我究竟做错了什么?
数据库版本:3.2
设置索引:
db.collection('history').ensureIndex( { '_id': 1, 'created': 1 }, { unique: true, background: true, w: 1, expireAfterSeconds: 60} ); // or db.collection('history').createIndex( { '_id': 1, 'created': 1 }, { unique: true, background: true, w: 1, expireAfterSeconds: 60} ); // history document var _history = { _id: new ObjectId(), created: new Date() };
collections历史,索引:
var historyCollectionIndex = [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "guardian_dev.history" }, { "v" : 1, "unique" : true, "key" : { "_id" : 1, "created" : 1 }, "name" : "_id_1_created_1", "ns" : "guardian_dev.history", "background" : true, "expireAfterSeconds" : 60 } ]
与创build索引相关的其他问题。
现在,可能发生两个条目具有相同的创build值,并因此,mongo现在抛出E11000重复键错误收集的错误。
是否可以添加创build和expireAfterSeconds,但创build不一定是uniq?
根据MongoDB网站 :
TTL索引是单个字段索引。 复合索引不支持TTL属性。
如果您删除了_id: 1
索引,而只是使用created
那么它应该按照您的预期行事
根据文件 ,TTL指数是一个单一的领域指数。 复合索引不支持TTL属性。 你应该创build索引如下:
db.collection('history').ensureIndex( {'created': 1 }, { unique: true, background: true, w: 1, expireAfterSeconds: 60} );
我已经testing过了,这个索引与你的问题不同,正确地清除了logging。