MongoDB C# 2 驱动程序 -- 无法从 BsonType 'Double' 反序列化 'String'

MongoDB C# 2 Driver -- Cannot deserialize 'String' from BsonType 'Double'

我是 MongoDB 的新手。我正在尝试检索查找集合中的所有条目。我收到以下错误:

{"An error occurred while deserializing the Symbol property of class Stock.Models.StockLookup: Cannot deserialize a 'String' from BsonType 'Double'."}

这是我收到错误的代码:

var stockLookups = _stockLookupRepository.GetAllAsync().Result.OrderBy(l => l.Symbol);

调用的方法如下:

public async Task<List<StockLookup>> GetAllAsync()
    {
        var result = await _collection.Find(sl => sl.Symbol != null).ToListAsync();
        return result;
    }

这是 StockLookup Class:

public class StockLookup
{
    public ObjectId Id { get; set; }
    public string Symbol { get; set; }
    public string Name { get; set; }
}

谁能帮我弄清楚问题出在哪里?任何帮助将不胜感激。

谢谢!

这条消息几乎说明了一切。您将 Symbol 定义为 string,而在数据库中有一些文档具有 double 类型 Symbol。它导致反序列化问题。 要找出这些非法数据,请尝试:

db.StockLookup.find({Symbol: {$type: 1}}
如果您的 collection 中有大量数据,

不要 执行此操作。这个没有索引,它会很慢。在这种情况下,您可能希望在检查类型之前添加其他条件以过滤掉数据。

有一个reference of all $types。您可能需要检查这些数据是如何进入您的 collection 的,否则会有更多非法数据。