我有一个包含现有数据的集合。我使用expireAfterSeconds参数为此集合添加了一个新索引:
2015-11-11T09:09:21.565+0100 I INDEX [conn120] build index on: cmon.cmon.interfaces_history properties: { v: 1, key: { expireAt: 1 }, name: "expireAt_1", ns: "cmon.cmon.interfaces_history", expireAfterSeconds: 1 }
2015-11-11T09:09:21.565+0100 I INDEX [conn120] building index using bulk method
2015-11-11T09:09:21.730+0100 I INDEX [conn120] build index done. scanned 8933 total records. 0 secs
2015-11-11T09:09:21.733+0100 I COMMAND [conn120] command cmon.$cmd command: createIndexes { createIndexes: "cmon.interfaces_history", indexes: [ { expireAfterSeconds: 1, name: "expireAt_1", key: { expireAt: 1 } } ] } keyUpdates:0 writeConflicts:0 numYields:0 reslen:113 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, MMAPV1Journal: { acquireCount: { w: 17870 }, acquireWaitCount: { w: 1 }, timeAcquiringMicros: { w: 886 } }, Database: { acquireCount: { W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 2 } } } 169ms
然后我添加了一个新条目, expireAt 值设置如下:
{
"_id" : ObjectId("56446521637140e419b881c7"),
"DeviceName" : "mx1",
"updateTime" : ISODate("2015-11-05T11:17:10.000Z"),
"expireAt" : ISODate("2015-11-12T11:10:13.257Z"),
"DomainName" : "domain.net",
"ifAdminStatus" : "up",
"ifName" : "3002",
"ifOperStatus" : "up"
}
此集合中的现有条目看起来完全相同,但未设置 expireAt 。 MongoDB不会从数据库中删除此条目。当我 在一个空的集合和测试数据库上对它进行了测试。
> db.serverStatus().metrics.ttl
{ "deletedDocuments" : NumberLong(0), "passes" : NumberLong(823) }
所以我怀疑它不会删除此条目,因为集合中还有其他条目没有此字段。或问题是其他什么?
答案 0 :(得分:1)
"expireAt" : ISODate("2015-11-12T11:10:13.257Z")
The expiry date and time you have specified are in the future. As of the time of this answer, the expiry will occur in two minutes from now. This is why the document is still not deleted from your collection. But it will be deleted very soon.
The date/time format you have specified has a Z
at the end, which enforces the UTC timezone.