Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoexport recently hours datas from mongodb

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.

like image 884
Sophia Avatar asked Nov 10 '22 13:11

Sophia


1 Answers

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 
like image 190
Luca Foppiano Avatar answered Nov 15 '22 13:11

Luca Foppiano