Search code examples
mongodbttl

I want to delete a specified field in the document after a particular period of time in MongoDB. How do I do it?


    {
        "_id" : ObjectId("5bdff3642397ba1731d93306"),
        "fullname" : "tim",
        "email" : "[email protected]",
        "password" : "$2a$10$WcSZsh/x26CfwLmFpudBcO6Rpxecpws977f/oRkZV6Vr0nmRUpOBO",
        "token" : "428pT4kvavcmmEbZGECUTeyjVEaA3NGc",
        "__v" : 0
}

    {
        "_id" : ObjectId("5bdff3642397ba1731d93986"),
        "fullname" : "tim",
        "email" : "[email protected]",
        "password" : "$2a$10$WcSZsh/x26CfwLmFpudBc09Rpxecpws977f/oRkZV6Vr0nmRUpOBO",
        "token" : "428pT4kvavcxdEbZGECUTeyjVEaA3NGc",
        "__v" : 0
}

I want to delete the "token" field only from both the documents after 5 minutes in MongoDB. Is it possible? How do I do it?


Solution

  • you need to do it from code level alternatively you can set generatedOn/lastAccessed property in the document if the time difference between 2 times is greater than 5 min then it must be treated as expired.