'MongoDB C# 2.0 Driver SetSerializationOptions

I have updated C# MongoDB Driver to 2.0

Now SetSerializationOptions is not working. Following is the old code.

cm.GetMemberMap(m => m.ArrayField)
      .SetSerializationOptions(
          new ArraySerializationOptions(
                new RepresentationSerializationOptions(BsonType.ObjectId)));

Please help me with the replacement for above code in 2.0 Driver?



Solution 1:[1]

The new documentation covers this here: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/#serialization-options.

You need to set a new serializer that is configured appropriately for your scenario.

Craig

Solution 2:[2]

Working code for MongoDB.Driver 2.14.1, might help anyone

public class Document 
{
    public string[] ArrayField {get; set;}
}

BsonClassMap.RegisterClassMap<Document>(cm =>
{
    cm.MapMember(m => m.ArrayField)
        .SetSerializer(
            new ArraySerializer<string>(new StringSerializer(BsonType.ObjectId))
        );
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Craig Wilson
Solution 2 Ahmed