mongodb replication without deleting data in secondary

丶灬走出姿态 提交于 2019-12-25 04:07:11

问题


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 ?


回答1:


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 ----

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"); // or any other valid conn string mirrordb.collection.insert(doc) // replace the above insert

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
}


来源:https://stackoverflow.com/questions/27726617/mongodb-replication-without-deleting-data-in-secondary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!