How to retrieve last update time of each document in MongoDB?

后端 未结 4 835
后悔当初
后悔当初 2021-02-12 12:18

I would like to know if there is a way to get the last update/modify time of data (i.e documents) in a collection in MongoDB. More clearly, I want to make a query to retrieve al

4条回答
  •  佛祖请我去吃肉
    2021-02-12 12:46

    You need to capture last update time yourself.

    For my application, I keep an AuditTrail object, which captures AuditEvents. These events occur on any insert, update, or delete of an object (delete is virtual in my system, just setting a flag).

    For each AuditEvent, I keep track of the date, authenticated user, db action, and a description filled in by the application. This is implemented in PersistentObject, so it is automatically called for any database action of any object saved in Mongo.

    This actual took very little time to implement, but provides both the ability to get the last update time, and also any other information that you may need for security and customer support for everything in Mongo.

提交回复
热议问题