I want to backup data of last day (Current Day-1) every day using cron tasks.
In the mongo shell, I can query the data using variables as below:
var start = new Date();
start.setHours(0,0,0,0);
start.setHours(start.getHours() - 24);
var end = new Date();
end.setHours(23,59,59,999);
end.setHours(end.getHours() - 24);
db.collection.find({created_at: {$gte: start, $lte: end}});
How to use the above process as a single query to run with mongodump from outside of the shell. What will be the query in the command below?
mongodump --db dbname --collection cname --query '{}' --out collection_01NOV2019
Best Answer
You can do it like this:
Putting all in one line is a bit ugly but possible:
Note, in crontab you have to write
+\%
rather than+%
. Perhaps it will be more clear when you write a 3-line shell script and execute this by cron.Simplified alternative: