Mongoexport in the past few hours from mongodb

I am new to music play. I want to export some data for the last hours from my databases. So it seems to me that I need to write the mongoexport command and include the date range in --query for this.

I am writing a bash file like this and trying 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:

 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 a connection to localhost , but cannot output data. If I remove the --query option, it may succeed and get all the data, but I need a query to subset the data for the last 3 hours.

Any ideas and help would be greatly appreciated. Thank you and you are better.

+5
source share
1 answer

using mongoexport you must provide a Date object with a timestamp.

Explained here: MongoDb timestamp

What you can write as a script looks like this (I'm pretty rusty with bash, of course, could be improved to stay on the same 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 
+1
source

All Articles