Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3653

Cannot deserialize a 'String' from BsonType 'ObjectId'

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.12.1, 2.12.2, 2.12.3
    • Component/s: Serialization
    • None
    • Environment:
      Linux, .NET Core 3.1, .NET 5.0.5 in ASP.NET Web application

      The first example from MongoDB documentation is not working.

      Here is the structure of my document:

      _id: ObjectId;

      is_approved: Boolean;

      text:String;

      The code of my entity is following:
      public class Joke
      {
      public ObjectId Id { get; set; }
      public string text { get; set; }
      public bool is_approved { get; set; }
      {color:#d4d4d4}}
      I'm trying the following code:
      IMongoClient
      client = new MongoClient(AppSettings.MongoCredentials);
      IMongoDatabase database = client.GetDatabase("joker");
      IMongoCollection<Joke> collection = database.GetCollection<Joke>("jokes");
      Joke joke = collection.Find(new BsonDocument()).First();
       

      The last line gives an exception:

      ---> System.FormatException: Cannot deserialize a 'String' from BsonType 'ObjectId'.

      at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)

      at MongoDB.Bson.Serialization.Serializers.SealedClassSerializerBase`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)

      at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)

      at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)

      at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)

      It is worth to mention that the bug can be reproduced in web application. In console app it works fine.

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            robert.lebovich@gmail.com Роберт Лейбович
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: