Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use mongoexport with a --query for ISODate

I have this query but i´m getting a syntax error: unexpected identifier

mongoexport --db ium --collection events \
  --query 'db.events.find({'created_at' : {
      $gte: ISODate("2016-03-01T00:00:00.001Z"),
      $lte: ISODate("2016-03-29T23:59:59:59.000Z")
    }, 
    "name" : "UPDATE_SUCCESS"})' \
 --out guille1_test.json

what can it be wrong?

like image 863
Chanafot Avatar asked Mar 30 '16 20:03

Chanafot


People also ask

How do I export data from MongoDB to JSON?

mongoexport command is used to export MongoDB Collection data to a CSV or JSON file. By default, the mongoexport command connects to mongod instance running on the localhost port number 27017. Field_name(s) - Name of the Field (or multiple fields separated by comma (,) ) to be exported.


2 Answers

You need to use "extended json" in queries with mongoexport. So the way to specify "dates" is with $date instead. And the --query is just the "query string" in JSON format. Not the whole command entered into the shell:

mongoexport --db ium --collection events \
  --query '{ 
    "created_at": { 
      "$gte": { "$date": "2016-03-01T00:00:00.001Z" },
      "$lte": { "$date": "2016-03-29T23:59:59.000Z" }
    },
    "name": "UPDATE_SUCCESS"
  }' \
  --out guile1_test.json

Note also the corrected date string in the $lte argument and of course the "quoting" use of '' around the body of the JSON argument and "" around the internal expressions and values. It s important that these types of quotes are different, as well as "shell arguments" should have their "outer" quotes as '', otherwise the "shell" tries to evaluate the expression contained.

like image 164
Blakes Seven Avatar answered Sep 28 '22 09:09

Blakes Seven


Best way to achieve it as following. Because new Date and IOSDate would be invalid literal for this command.

For remote host

mongoexport --host {{host}} --username {{username}} --password {{passord}} --authenticationDatabase admin --db {{Database}} --collection {{collection Name}} --query '{ "date" : { "$gt" : {"$date":"2019-10-31T00:00:00.000Z"}  } }'  --type json --out {{path of directory where you would want to export file.}}

For local host

 mongoexport  --db {{Database}} --collection {{collection Name}} --query '{ "date" : { "$gt" : {"$date":"2019-10-31T00:00:00.000Z"}  } }'  --type json --out {{path of directory where you would want to export file.}}
like image 26
Abhimanyu Kmar Avatar answered Sep 28 '22 11:09

Abhimanyu Kmar