简体   繁体   中英

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM