我正在使用 MongoDB CSharp 驱动程序来检索数据集合。不幸的是,其中一些数据不是很“干净”,因为某些字段同时是字符串和整数。正如预期的那样,反序列化过程每次遇到与正确类型不匹配的数据时都会引发异常。
我在网络上和 StackOverflow 上进行了广泛的搜索,但没有运气。有没有办法在反序列化时简单地处理/忽略异常并移至下一条记录?
我的代码看起来与此类似:
MongoClient client = new MongoClient(MongoConnectionString);
MongoServer server = client.GetServer();
MongoDatabase database = server.GetDatabase(DatabaseName);
MongoCollection<BookingLines> bookingLines = database.GetCollection<BookingLines>("bookinglines");
var query = from e in bookingLines.AsQueryable() where e.Status != "void" select e;
代码通常会在上面的最后一行抛出错误。我还尝试将 [BsonIgnore] 属性添加到某些字段。
任何想法或建议都会非常有帮助。
您能够在插入之前清理数据吗?一般来说,就像 SQL 一样,您不想将垃圾放入数据库中。
另一种方法(如果您不需要这些不可靠的字段)就是不将它们包含在 BookingLines 对象中?
您可以尝试使用
BsonClassMapSerializer<T>
作为自定义解串器:
public class CustomIgnoreErrorsSerializer<T> : BsonClassMapSerializer<T>
{
public CustomIgnoreErrorsSerializer(BsonClassMap classMap) : base(classMap) { }
public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
try
{
return base.Deserialize(context, args);
}
catch (Exception ex)
{
while (context.Reader.State != BsonReaderState.EndOfDocument)
{
if (context.Reader.State == BsonReaderState.Name)
context.Reader.SkipName();
if (context.Reader.State == BsonReaderState.Value)
context.Reader.SkipValue();
if (context.Reader.State == BsonReaderState.Type)
context.Reader.ReadBsonType();
}
context.Reader.ReadEndDocument();
return default(T);
}
}
}
您需要做一些额外的工作,包括将
BsonReader
跳过到当前文档的末尾。
然后要注册这个反序列化器,因为它是一个
BsonClassMapSerializer
类型,您需要传递该类型的类映射,并且您必须为您希望应用到的每个集合类型执行此操作。
var cm = BsonClassMap.LookupClassMap(typeof(MyType));
BsonSerializer.RegisterSerializer(new CustomIgnoreErrorsSerializer<MyType>(cm));