简体   繁体   中英

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

I am new to MongoDB. I am trying to retrieve all entries in a lookup Collection. I am receiving the following error:

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

This is the code that I am receiving the error on:

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

Here is the method being called:

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

Here is the StockLookup Class:

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

Can anyone help me figure out what the problem is? Any help would be greatly appreciated.

Thanks!

The message almost explained itself. You defined the Symbol as a string , while in database there's some document that has a double type Symbol . It's causing a deserializing issue. To find out these illegal data, try:

db.StockLookup.find({Symbol: {$type: 1}}

DON'T do this if you have a big amount of data in your collection. There's no index on this and it's going to be slow. In this case you may want to add other conditions to filter out data before checking the type.

There is a reference of all $types . You may want to check how these data goes into your collection otherwise there will be more illegal data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM