如何在 MongoDB 中的长时间戳上创建 TTL 索引

Adr*_*ian 6 javascript mongodb nosql

在我的 mongo 数据库中,我有字段时间戳,它在时间戳中保存创建时间,即:"timestamp": 1544029233021 我想在这个字段上创建 TTL 索引,但在文档示例中完成的"createdAt": new Date(),ISODate("2018-12-13T17:00:10.433Z")

是否有可能以任何方式使 TTL 索引在时间戳字段上工作?

因为这不起作用:

db.coll.createIndex( { "timestamp": 1 }, { expireAfterSeconds: 3600 } )
Run Code Online (Sandbox Code Playgroud)

Ada*_*son 13

The documents aren't being expired because the timestamp value is an integer.

TTL indexes will only work on documents where the indexed field is a Date or holds an array of Dates:

如果文档中的索引字段不是日期或保存日期值的数组,则文档不会过期。

( https://docs.mongodb.com/manual/core/index-ttl/#expiration-of-data )