简体   繁体   English

归档mongodb集合

[英]Archiving a mongodb collection

I'm using java Spring and spring data for mongodb. 我正在使用Java Spring和mongodb的spring数据。

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. 我正在寻找解决方案,但我能找到有关完整数据库备份的话题。

What is the best way to keep the collection updated to only the last 3 months? 将收藏集更新到最近三个月的最佳方法是什么? (weekly cron?) How to save the collection archive? (每周cron?)如何保存收藏档案? I think mongodump is an overkill. 我认为mongodump太过分了。

Both mongoexport and mongodump support a -q option to specify a query to limit the documents that will be deleted. mongoexport和mongodump均支持-q选项以指定查询以限制将要删除的文档。 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>}}" 

And then delete the old data. 然后删除旧数据。

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: 或者,您可以使用带有ttl索引的集合中的任何一种方法通过cron进行定期快照,这样您就不必自己修剪它-mongodb将自动删除较旧的数据:

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

This will keep deleting any document older than 91 days based on a createdAt field in the document 根据文档中的createdAt字段,这将继续删除任何超过91天的文档

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

With mongoexport you can backup a single collection instead of the whole database. 使用mongoexport,您可以备份单个集合而不是整个数据库。 I would recommand a Cron-Job (like you sad) to export the data and ceep the database limited to the Documents of the last 3 months my removing oder documents. 我会建议Cron-Job(像您一样伤心)导出数据,并在删除其他文档后,将数据库限制在最近3个月的文档范围内。

mongoexport -d databasename -c collectionname -o savefilename.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM