RavenDB transformers will lower-case your IDs

Here’s a nice little gotcha. When RavenDB (v3.0.30155) returns data from the database, it will generally preserve the casing of the IDs (e.g. an ID of MyDocuments/1 will be returned as such), even though RavenDB itself doesn’t care about casing (e.g. if you run Session.Load<MyDocument>("mydocuments/1"), you’ll still get back the right document). However, if you write a transformer like this:

public class MyDocumentTransformer : AbstractTransformerCreationTask<MyDocument>
{
    public MyDocumentTransformer()
    {
        TransformResults = docs => docs.Select(d => new TransformedDocument
        {
            Id = d.Id
        });
    }
}

You’ll notice that when you use this transformer, any returned TransformedDocuments will have lowercase IDs like mydocuments/1. Although RavenDB doesn’t care about this, we had some code further down the line that (unfortunately) did rely on casing, which then started to fail. Fortunately, since transformers are just running C# code on the server, it’s easy to fix with something like this:

public class MyDocumentTransformer : AbstractTransformerCreationTask<MyDocument>
{
    public MyDocumentTransformer()
    {
        TransformResults = docs => docs.Select(d => new TransformedDocument
        {
            Id = d.Id.replace("mydocuments", "MyDocuments")
        });
    }
}

Which will restore your IDs to their proper-cased glory.

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.

export_button

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:

  1. Rename the CSV file to match the desired collection name, for example Cars.csv would import into a collection called Cars

  2. 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 to id, 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.