Search code examples
pythonmongodbscrapymongoexport

Export MongoDB to CSV Using File Name Variable


I have a MongoDB that houses data from a web scrape that runs weekly via Scrapy. I'm going to setup a cron job to run the scrape job weekly. What I would like to do is also export a CSV out of MongoDB using mongoexport however I would like to inject the current date into the file name. I've tried a few different methods without much success. Any help would be greatly appreciated! For reference, my current export string is: mongoexport --host localhost --db glimpsedb --collection scrapedata --csv --out scrape-export.csv --fields dealerid,unitid,seller,street,city,state,zipcode,rvclass,year,make,model,condition,price

So, ideally the file name would be scrape-export-current date.csv

Thanks again!


Solution

  • Replace --out scrape-export.csv in your command with --out scrape-export-$(date +"%Y-%m-%d").csv

    It'll create filenames in the format scrape-export-2016-09-05