MongoDB Custom Serializer to avoid _t being added collection, throws ReadEndArray Error?

Also asked here: https://groups.google.com/forum/#!topic/mongodb-user/iOeEXbUYbo4

I think your better bet in this situation is to use a custom discriminator convention. You can see an example of this here: https://github.com/mongodb/mongo-csharp-driver/blob/v1.x/MongoDB.DriverUnitTests/Samples/MagicDiscriminatorTests.cs. While this example is based on whether a field exists in the document, you could easily base it on what type the field is (BsonType.Int32, BsonType.Date, etc...).


Basing on @Craig Wilson answer, to get rid off all discriminators, you can:

public class NoDiscriminatorConvention : IDiscriminatorConvention
    {
        public string ElementName => null;

        public Type GetActualType(IBsonReader bsonReader, Type nominalType) => nominalType;

        public BsonValue GetDiscriminator(Type nominalType, Type actualType) => null;
    }

and register it:

BsonSerializer.RegisterDiscriminatorConvention(typeof(BaseEntity), new NoDiscriminatorConvention());