[英]delete data from mongodb collection when date was get expired
I had a collection which holds user session and it has a field expiry date. 我有一个保存用户会话的集合,它有一个字段到期日期。 I am looking for a trigger like function in MongoDB which triggered when any of the records dates get expired and I can delete that record.
我正在MongoDB中寻找类似函数的触发器,该触发器在任何记录日期过期时触发,并且我可以删除该记录。
here is how my collection look like 这是我的收藏集的样子
{
_id:"123",
sessionId:"dummy",
token:"asdasd",
deviceId:"123213",
expiryDate:"date"
}
Assuming expiryDate
is an actual Date
type and not a string, you can use a TTL index to do this. 假设
expiryDate
是实际的Date
类型而不是字符串,则可以使用TTL索引执行此操作。
db.mycoll.createIndex({expiryDate: 1}, {expireAfterseconds: 0})
Will instruct MongoDB to automatically delete documents from the collection once the current time is greater than expiryDate
. 一旦当前时间大于
expiryDate
指示MongoDB自动从集合中删除文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.