Javascript MongoDB expireAfterSeconds不';不要删除文档
我希望MongoDB在通过%秒后清除其集合中的数据。我正在设置索引,但集合在一段时间后不会被清除,所有文档仍然存在 我做错了什么 数据库版本:3.2 设置索引:Javascript MongoDB expireAfterSeconds不';不要删除文档,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我希望MongoDB在通过%秒后清除其集合中的数据。我正在设置索引,但集合在一段时间后不会被清除,所有文档仍然存在 我做错了什么 数据库版本:3.2 设置索引: db.collection('history').ensureIndex( { '_id': 1, 'created': 1 }, { unique: true, background: true, w: 1, expireAfterSeconds: 60} ); // or db.collection('hist
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 duplicate key error collection错误
是否可以添加created和expireAfterSeconds,但created不必是uniq?根据:
TTL索引是一个单字段索引。复合索引不支持TTL属性
如果您删除\u id:1
索引,而只使用创建的
,那么它的行为应该与您预期的一样,根据,TTL索引是单个字段索引。复合索引不支持TTL属性。您应按如下方式创建索引:
db.collection('history').ensureIndex(
{'created': 1 },
{ unique: true, background: true, w: 1, expireAfterSeconds: 60}
);
我已经对它进行了测试,该索引与您问题中的索引不同,它正确地清除了记录。问题的第二部分:set unique:false