0

I am trying to take mongodump of a collections of last 24 hours using bash but getting errors as i am unable to use custom date in query parameter of mongodump statement.

timeInMs=$(expr "$(date +'%s%3N')" - 86400000)

mongodump -u user -p password --authenticationDatabase admin --db dbname -c collection --query '{startTime:{$gte:new Date(${timeInMs})}}'

timeInMs is as expected (time in ms 24 hrs ago) but problem is getting query right. Lots of hit & trial used but no success yet. Have used following :

'{startTime:{$gte:{"$date":"${timeInMs}"}}}'

"{startTime:{$gte:new Date\"(${timeInMs})\"}}"

'{startTime:{$gte:new Date("${timeInMs}")}}'

vashishatashu
  • 7,720
  • 7
  • 29
  • 34

1 Answers1

1

You need to get your quotes properly:

timeInMs=$(expr "$(date +'%s%3N')" - 86400000)
mongodump -u user -p password --authenticationDatabase admin --db dbname -c collection --query '{startTime:{$gte:new Date('"$timeInMs"')}}'

For better readability:

mongodump -u user \
-p password \
--authenticationDatabase admin \
--db dbname -c collection --query  '{startTime:{$gte:new Date('"$timeInMs"')}}'
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51
  • new Date takes number as parameter in case of ms , but these quotes result in string conversion. – vashishatashu Feb 27 '15 at 04:41
  • I assumed you have timeInMs set, if you do that's what Mongod dump will get: `mongodump -u user -p password --authenticationDatabase admin --db dbname -c collection --query {startTime:{$gte:new Date(1424940476866)}}` – Tiago Lopo Feb 27 '15 at 08:57