如何在没有objectid字段的mongodb集合上查找上次更新/插入/删除操作时间 [英] How to find last update/insert/delete operation time on mongodb collection without objectid field

查看:610
本文介绍了如何在没有objectid字段的mongodb集合上查找上次更新/插入/删除操作时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDb数据库中有一些未使用的集合。我必须找出CRUD操作什么时候对数据库中的集合。我们有自己的_id字段,而不是mongo的默认object_id。我们没有在集合中的任何时间归档找出修改时间。有什么办法来找出mongodb从元数据中的集合的修改时间?有没有任何数据字典信息,如在oracle中找到这个?请提供一些想法/解决方法

解决方案

简要说明:MongoDB有一个灵活模式。只需添加日期字段。



让我们调用这个字段 mtime



因此,在向您的模式定义添加日期字段之后,我们将在新字段上按降序生成索引:

  db.yourCollction.createIndex({mtime:-1})



现在找到集合的最后一个mtime很容易:

  db.yourCollection.find({mtime:{$ exists:true}})。sort({mtime: -  1})。 

为每个集合执行此操作。当上述查询未返回您为清除集合而定义的时间范围内的值时,只需将其删除即可,因为自您引入mtime字段以来,它没有被修改。



清除集合后,可以从模式定义中删除 mtime 字段。要从文档中删除它,您可以运行一个简单的查询:

  db.yourCollection.update (
{mtime:{$ exists:true}},
{$ unset:{mtime:}},
{multi:true}


I have some unused collections in the MongoDb database. I've to find out when the CRUD operations done against collections in the database. We have our own _id field instead of mongo's default object_id. We dont have any time filed in the collections to find out the modification time. is there any way to find out the modification time of collections in mongodb from meta data? Is there any data dictionay informations like in oracle to find out this? please give some idea/workarounds

解决方案

To make a long story short: MongoDB has a flexible schema. Simply add a date field. Since older entries don't have it, they can not be the last entry.

Let's call that field mtime.

So after adding a date field to your schema definition, we generate an index in descending order on the new field:

db.yourCollction.createIndex({mtime:-1})

Finding the last mtime for a collection now is easy:

db.yourCollection.find({"mtime":{"$exists":true}}).sort({"mtime":-1}).limit(1)

Do this for every collection. When the above query does not return a value within the timeframe you defined for purging a collection, simply drop it, since it has not been modified since you introduced the mtime field.

After your collections are cleaned up, you may remove the mtime field from your schema definition. To remove it from the documents, you can run a simple query:

db.yourCollection.update(
  { "mtime":{ $exists:true} },
  { "$unset":{ "mtime":""} },
  { multi: true}
)

这篇关于如何在没有objectid字段的mongodb集合上查找上次更新/插入/删除操作时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆