dongna9185 2017-11-04 02:04
浏览 88
已采纳

进行n天后删除MongoDB文档

I want to delete user accounts after 3 days if they don't verify their email. As a scheduler would be inefficient, I'm searching for a way to schedule this delete in MongoDB. I also need a way to cancel it, if the user verifies the email.

I'm using mgo as api for MongoDB and I'm running latest Go (1.9).

  • 写回答

1条回答 默认 最新

  • doupu0619 2017-11-04 03:52
    关注

    This can be achieved in MongoDB version 3.4

    You can use mongodb's TTL index along with partial index expression.

    Try adding following index on users collection:

    db.users.ensureIndex(
    { created_at:1}, 
    { expireAfterSeconds:259200, 
      partialFilterExpression:{"verified" : false}})
    

    This TTL index with delete all those users who are not verified and it has been 3 days since they were added.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?