Exporting and importing CSV data in RavenDB
In today’s episode of not-entirely-obvious RavenDB functionality, I was trying to use the CSV import/export functionality in order to transfer a collection from our live to our test DB, since at the time of writing the “export database” functionality seems to be broken if you try to use it to export only specified collections.
Exporting is obvious enough – simply navigate to a collection you’re interested in exporting, optionally select the documents you want to export by ticking the checkboxes, and then click the export CSV button as shown below.
This will download a file called export.csv
. You can then go to Tasks -> CSV Import to import the data into another database. However, if you just import the CSV as it came out of RavenDB, you’ll find yourself creating a new collection called “export”, with all your documents from the CSV imported with auto-generated GUIDs for the document IDs.
If, instead, you’d like to import the documents into a collection with the same name and IDs as the original, as I did, you’ll need to do the following:
- Rename the CSV file to match the desired collection name, for example
Cars.csv
would import into a collection calledCars
-
Column names in the CSV with an
@
sign before them will be ignored on import. If you want to preserve IDs, open the CSV and change the@id
header toid
, and the IDs will be included when importing.
Please note however that RavenDB will not update the HILOs if you import data from a CSV, so you’ll need to make sure that you update those yourself separately to avoid any problems when you try to create new documents in the same collection.