Mongodb – How to use variables in a mongodump query

backupmongodbmongodumpnosql

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:

start="new Date(1000*$(date -d `date -d yesterday +%F` +%s))"
end="new Date(1000*$(( $(date -d `date +%F` +%s) - 1 )))"
query="db.collection.find({created_at: {\$gte: $start, \$lte: $end}});"

echo $query
db.collection.find({created_at: {$gte: new Date(1000*1575586800), $lte: new Date(1000*1575673199)}});

Putting all in one line is a bit ugly but possible:

mongodump --db dbname --collection cname --query 'db.collection.find({created_at: {$gte: '"new Date(1000*$(date -d `date -d yesterday +%F` +%s))"', $lte: '"new Date(1000*$(( $(date -d `date +%F` +%s) - 1 )))"'}})' --out collection_01NOV2019

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:

start="new Date('`date -d yesterday +%FT00:00:00`')"
end="new Date('`date -d yesterday +%FT23:59:59`')"
query="db.collection.find({created_at: {\$gte: $start, \$lte: $end}});"

echo $query
db.collection.find({created_at: {$gte: new Date('2019-12-05T00:00:00'), $lte: new Date('2019-12-05T23:59:59')}});