Search code examples
mongodbmongodb-querymongodb-.net-driverexport-to-csvmongoexport

MongoExport - add date to filename


Is there a way to append today's date formatted as "mm-dd-yyy" to a file name in a mongoexport command?

My command looks like this: mongoexport --username x --password y --host a1.mongolab.com --db mydb --collection Contacts --type=csv --fields "FirstName,LastName,EmailsAddress" --out "c:\test\out.csv"

I want the file to be named as: out_04-06-2016.csv


Solution

  • Other than the obvious of adding in a hardcoded date to your filename, you could do it as a batch file.

    1) Create a text file with a .cmd extension.

    2) add this code, save and then double click to run.

    set mydate=!date:~10,4!!date:~7,2!!date:~4,2!
    mongoexport --username x --password y --host a1.mongolab.com --db mydb --collection Contacts --type=csv --fields "FirstName,LastName,EmailsAddress" --out "c:\test\out_%mydate%.csv"