Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB expireAfterSeconds不';不要删除文档_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript MongoDB expireAfterSeconds不';不要删除文档

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

我希望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 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