Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb export and remove docs

I would like to run a unix cron every day that does:

  • export all docs that are over 3 month old to a document
  • remove from the same docs from the collections.

For the export part I use:

mongoexport --db mydb --collection mycollection\  --query "`./test2.sh`" --out ./test2.json

and the "./test2.sh" file contains:

#!/bin/bash
d=`date --date="-3 month" -u +"%Y-%m-%dT%H:%M:%SZ"`
echo '{ "timeCreated": { "$lte": { "$date": "'$d'" } } }'

For the remove part I can do:

mongo mydb /home/dev/removeDocs.js

removeDocs.js:

var d = new Date();
d.setMonth(d.getMonth()-3);
db.GameHistory.remove({ timeCreated: { $lte: d} });

How can I synchronize the 2 commands? I want to run the remove commend after the export finished. can I merge the 2 to the same cron?

like image 263
lior Avatar asked Sep 20 '25 06:09

lior


1 Answers

Yes, you can.

The easiest way is to merge both commands into single one-liner:

mongoexport --db mydb --collection mycollection\  --query "`./test2.sh`" --out ./test2.json && mongo mydb /home/dev/removeDocs.js

But I would recommend you to create shell script to archive your database:

#!/bin/bash
set -e # stop on first exception
mongoexport --db mydb --collection mycollection\  --query "`./test2.sh`" --out ./test2.json
mongo mydb /home/dev/removeDocs.js

If you want to append each new chunk of exported data, you should replace --out with standard unix stdio redirection:

mongoexport --db mydb --collection mycollection\  --query "`./test2.sh`" >> ./test2.json
like image 161
Leonid Beschastny Avatar answered Sep 22 '25 22:09

Leonid Beschastny