I needed to export a set of data from a mongoDB collection based on their objectIDs’ (_id) timestamp using mongoexport. The mongoexport documentation is everything but helpful on the subject so I had to find a workaround to answer this simple question : “export all documents inserted yesterday on this collection in a CSV format”.

Relevant mongoexport options

–host : specify the mongoDB host

–username / –pasword : if you’re using authentication on your server

-d : database to use

-c : collection to use

–fields : fields you want to export (omit for all)

–query : the actual query selecting the result set you want to export

–csv : export in a CSV format

The date range query workaround

So the hard part is to actually ask mongoexport to only return the documents in the desired time frame using an objectID compliant query. I overcame this problem using a simple but efficient python script generating the query for me.