如何使用 MONK 设置 TTL 使文件过期?

How to set TTL to expire documents using MONK?

我阅读了 MongoDB documentation 以了解如何使用生存时间 属性 使文档过期。

文档说:

To expire data after a specified number of seconds has passed since the indexed field, create a TTL index on a field that holds values of BSON date type... For example, the following operation creates an index on the log_events collection’s createdAt field and specifies the expireAfterSeconds value of 3600 to set the expiration time to be one hour after the time specified by createdAt

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )

When adding documents to the log_events collection, set the createdAt field to the current time:

db.log_events.insert( {
   "createdAt": new Date(),
   "logEvent": 2,
   "logMessage": "Success!"
} )

如何使用节点 Monk 来做到这一点?

应该是:

db.log_events.ensureIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 })

关于 ensureIndex

的文档

Ensures that indexes exist, if it does not it creates it