1
votes

"An error occurred while deserializing the Property1 property of class Class1: ReadString can only be called when CurrentBsonType is String, not when CurrentBsonType is ObjectId."

I put the bsonid on the string guid of the class and mapped the class and serialized it, but it is giving me this error when I tried to deserialize my object back to the class from a bsondocument.

How should I fix this?

public class Class1 
{
      [BsonId]
      public string ClassGuid { get; private set; }
}

Then map the class:

if (!BsonClassMap.IsClassMapRegistered(typeof(WordOntology)))
{
      BsonClassMap.RegisterClassMap<WordOntology>();
}

Then deserialize

MongoCursor<Class1> _returnResults = _collection.FindAs<Class1>(_query);

Then loop:

foreach(BsonDocument _document in _returnResults) 
{
    //exception here
}
1

1 Answers

1
votes

You probably have existing documents in your collection that do not match your class definition. You can look for them using the mongo shell (look for documents that have an ObjectId where you are expecting a string to be).

If that doesn't explain it, can you provide your class declaration and a sample document (displayed using the mongo shell) that won't deserialize properly?