gpt4 book ai didi

javascript - 如何使用MONK设置TTL使文档过期?

转载 作者:行者123 更新时间:2023-12-03 06:05:37 25 4
gpt4 key购买 nike

我读了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!"
} )

如何使用 Node Monk 做到这一点?

最佳答案

应该是:

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

关于 ensureIndex 的文档

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

关于javascript - 如何使用MONK设置TTL使文档过期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39563938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com