归档 mongodb 集合 [英] Archiving a mongodb collection

查看:68
本文介绍了归档 mongodb 集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 mongodb 使用 java Spring 和 spring 数据.

I'm using java Spring and spring data for mongodb.

我有一个集合,需要包含过去 3 个月的文档,但所有文档都应该以某种方式保存(也许可以保存到文件中?).我正在寻找解决方案,但我只能找到有关完整数据库备份的讨论.

I have a collection that needs to contain only documents from the last 3 months but all the documents should be saved in some way (maybe expoet to a file?). I'm looking for solution but all i can find talks about full DB backup.

让收藏只更新到最近 3 个月的最佳方法是什么?(每周计划?)如何保存收藏档案?我认为 mongodump 是一种矫枉过正.

What is the best way to keep the collection updated to only the last 3 months? (weekly cron?) How to save the collection archive? I think mongodump is an overkill.

推荐答案

mongoexport 和 mongodump 都支持 -q 选项来指定一个查询来限制将被删除的文档.两者的选择更多地取决于您希望数据以何种格式存储.

Both mongoexport and mongodump support a -q option to specify a query to limit the documents that will be deleted. The choice for either is more of a function of what format you'd like the data to be stored in.

假设您有一个带有时间戳字段的集合.您可以运行其中之一(在尖括号中填写所需的名称和时间):

Let's assume that you have a collection with a timestamp field. You could run either one of these (filling in the required names and times in the angle brackets):

mongoexport -d <yourdatabase> -c <yourcollection> -q "{ timestamp: { \$gt: <yourtimestamp>}}" -o <yourcollection_export_yourtimestamp>.json
mongodump -d <yourdatabase> -c <yourcollection> -q "{ timestamp: { \$gt: <yourtimestamp>}}" 

然后删除旧数据.

或者,您可以通过 cron 使用任一方法对具有 ttl 索引的集合进行定期快照,这样您就不必自己修剪它 - mongodb 将自动删除旧数据:

Alternatively you could take periodic snapshots via cron with either method on a collection with a ttl index so that you don't have to prune it yourself - mongodb will automatically delete older data:

db.collectioname.ensureIndex( { "createdAt": 1 }, { expireAfterSeconds: 7862400 } )

这将根据文档中的 createdAt 字段继续删除任何超过 91 天的文档

This will keep deleting any document older than 91 days based on a createdAt field in the document

http://docs.mongodb.org/manual/tutorial/expire-data/

这篇关于归档 mongodb 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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