【发布时间】:2016-01-29 12:52:17
【问题描述】:
我希望 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,但 created 不一定是 uniq?
【问题讨论】:
-
问题的第二部分:设置唯一性:false
标签: javascript node.js mongodb