问题描述
我想知道是否有一种方法可以获取MongoDB中集合中数据(即文档)的最后更新/修改时间.更清楚地说,我想查询一个特定时间之后更新的所有文档.
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 all documents updated after a particular time.
在MongoDB中是否有任何可能的方法来检索上次修改的时间戳记?
Is there any possible ways to retrieve this last modified timestamp in MongoDB?
注意:对于新创建的文档,我知道我们可以从objectId检索时间戳,但是对于更新,该ID将是相同的. MongoDB是否在任何地方存储每个文档的最后更新时间?
Note: For newly created documents, I know that we can retrieve the timestamp from the objectId, but for an update, the id is going to be same. Does MongoDB store the last update time for each document anywhere?
我正在使用吗啡作为Java驱动程序,因此,如果有任何可行的吗,请让我知道.
I am using morphia as the java driver, so if there is any possible way from morphia, please let me know.
推荐答案
您需要自己捕获上次更新时间.
You need to capture last update time yourself.
对于我的应用程序,我保留一个AuditTrail对象,该对象捕获AuditEvents.这些事件发生在对象的任何插入,更新或删除(删除在我的系统中是虚拟的,只需设置一个标志).
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).
对于每个AuditEvent,我都会跟踪日期,经过身份验证的用户,数据库操作以及应用程序填写的描述.这是在PersistentObject中实现的,因此会自动调用Mongo中保存的任何对象的任何数据库操作.
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.
此实用程序花了很少的时间来实现,但是不仅提供了获取最新更新时间的能力,而且还提供了您可能需要的其他安全性信息以及对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.
这篇关于如何在MongoDB中检索每个文档的上次更新时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!