[英]MongoDB expireAfterSeconds doesn't remove documents
我希望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()
};
收集历史,索引:
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
}
]
与创建索引相关的其他问题。
现在,可能会发生两个条目具有相同的创建值,因此,mongo现在抛出错误的E11000重复键错误集合。
是否可以添加created和expireAfterSeconds,但是创建的不一定是uniq?
根据文档 ,TTL索引是单个字段索引。 复合索引不支持TTL属性。 您应该按如下方式创建索引:
db.collection('history').ensureIndex(
{'created': 1 },
{ unique: true, background: true, w: 1, expireAfterSeconds: 60}
);
我测试了它,这个索引与你问题中的索引不同,正确地清除了记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.