I am a new beginner of mongodb. I want to export some data in recent hours from my databases. So, I think I need to write mongoexport command and include date range in --query
options to do it.
I write a bash file like this and try to run it:
#!/bin/bash
mongoexport --host localhost:27017 --db copy --collection txt --csv --fields x1,x2,x3...,date --query '{ "date" : {$gt:new Date(new Date() - 1000*60*60*3)} }' --out home/data.csv
But I get the results says:
connected to: localhost:27017
assertion: 16619 code FailedToParse: FailedToParse: Expecting '}' or ',': offset:25 of:{ "date" : {$gt:new Date(new Date() - 1000*60*60*3)} }
It sees connect to localhost but cannot output the data. If I remove --query
option, this can run successfully and get the whole data, but I must need the query to subset the data in recently 3 hours.
Any ideas and help will be highly appreciated. Thank you and Best.
with mongoexport you have to provide the Date object a timestamp.
An explaination is answered here: MongoDb timestamp
What you can write as script is something like this (I'm rather rusty with bash, surely can be improved to stay on a single line):
timestamp=$(date +%s)
let total=$timestamp*1000-3600*1000*3
mongoexport --host localhost:27017 --db copy --collection txt --csv --fields x1,x2,x3...,date --query '{ "date" : {$gt:new Date('$total')} }' --out home/data.csv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With