1

Does the MongoDB C# driver have the ability to map two field names from the server to a single field in the C# class?

The reason that we need this is that we've renamed a field in our code and would like to support both the old and new field name for a period of time.

This is a typical data migration scenario and it would be very helpful if we could establish a pattern for doing this via mapping without having to add special version logic to the data entity class or to the logic code.

We're currently using the official c# driver v2.3 with server v3.2.

GaTechThomas
  • 5,421
  • 5
  • 43
  • 69

1 Answers1

1

I don't think that is very much possible. What you can do is

  1. Create a new property that is not saved in the bson. Use BsonIgnore tag thusly and populate the data from both of your property and expose that in the JSON response you are providing. Use JsonIgnore to make sure you provide proper ignoring directives there.

  2. Go for migration techiniques in Mongodb. You can use a migration tool like this one. As you are using the C# driver 2.3.0 I would presume you want to use that and you can use my fork here for C# driver 2.3.0 compatibility.

Code sample for number 1:

class Data
{
    [JsonIgnore]
    public string OldProperty { get; set; }

    public string NewProperty
    {
        get
        {
            // Return this one or the old one based on your logic 
        }
        set;
    }
}
Swagata Prateek
  • 1,076
  • 7
  • 15