mongodb复制而不删除辅助数据 [英] mongodb replication without deleting data in secondary

查看:63
本文介绍了mongodb复制而不删除辅助数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器中有一个 mongod 实例,旧数据每天都被删除(我只保留数据 30 天).另一方面,我希望在我的客户端中有一个包含整个数据的数据库镜像副本,并且不想在我的客户端中应用任何删除指令(我需要将所有旧数据保留在客户端中,而这些旧数据已在服务器中删除).

i have a mongod instance in server and older data has been deleted every day (i keep data just for 30 days) . in the otherside i want to have a mirror copy of database in my client with whole data and don't want to apply any remove instructions in my client (i need to keep all of the old data in client which is deleted in server).

哪种类型的复制对我有帮助?

which type of Replication would help me ?

推荐答案

如果您只需要数据(相对于真正的数据库镜像),那么请考虑查看基于 automysql 备份的备份脚本 auto mongobackup 每天、每周、每月轮换备份.在删除数据之前运行它(假设它是一个批处理).它简单且愿意适应.如果您需要镜像,可以根据导出编写 mongo 导入脚本.

if data is your only need (vs a bonafide db mirror) then consider looking at this backup script based on automysql backup auto mongobackup that does rotating daily, weekly, monthly backups. run this just before you delete your data (assuming its a batch process). its simple and willing to be adapted. one might write a mongo import script based on the export if you need a mirror.

------ 编辑----

------ edit ----

给定的评论考虑一个 mongo shell 脚本 定期执行(使用 crontab).

given comments consider a mongo shell script that is executed periodically (using crontab).

// mirror every 10 seconds
db.collection.find({"is_mirrored" : { $exists: false }  }).forEach(function(doc) {  
   db.mirror.insert(doc);
   db.collection.update({doc._id}, {"is_mirrored": true}, {upsert: false});
}

创建镜像表"而不是镜像数据库.镜像数据库需要一个连接,所以添加

that creates a 'mirrored table' not a mirrored db. a mirrored db needs a connection so add along the lines of

var mirrordb = connect("localhost:27020/mirrored_db");//或任何其他有效的连接字符串mirrordb.collection.insert(doc)//替换上面的insert

var mirrordb = connect("localhost:27020/mirrored_db"); // or any other valid conn string mirrordb.collection.insert(doc) // replace the above insert

另一个删除 30 天旧的东西.

and another to remove 30 day old stuff.

var now = ISODate();
var ago = now - (30 * 86400 seconds); //some javascript to subtract 30 days

// find all date that is less (or older) than 30 days old
db.collection.find({"created_date" : {$lt : ago }}).forEach(function(doc) {  
   db.collection.remove(doc._id);        // remove the 30 day old data
}

这篇关于mongodb复制而不删除辅助数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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