title: FormatException: Cannot deserialize a 'Guid' from BsonType 'ObjectId'
body: <p>I have been going crazy this last days.</p>
  <p>I'm trying to use string Guids as _id's in my MongoDb, with the latest version of the driver. I stumbled with this tutorial, which shed me some light...</p>
  <p><a href="https://kevsoft.net/2020/06/25/storing-guids-as-strings-in-mongodb-with-csharp.html" rel="nofollow noreferrer">Storing GUIDs as strings in MongoDB with C#</a></p>
  <p>But I am getting this error: <strong>FormatException: Cannot deserialize a 'Guid' from BsonType 'ObjectId'.</strong></p>
  <pre><code>   MongoDB.Bson.Serialization.Serializers.GuidSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  MongoDB.Bson.Serialization.Serializers.SerializerBase&lt;TValue&gt;.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
  MongoDB.Bson.Serialization.BsonClassMapSerializer&lt;TClass&gt;.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
  </code></pre>
  <p>The stack trace:</p>
  <pre><code> at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
     at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
     at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
     at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
     at MongoDB.Driver.Core.Operations.CursorBatchDeserializationHelper.DeserializeBatch[TDocument](RawBsonArray batch, IBsonSerializer`1 documentSerializer, MessageEncoderSettings messageEncoderSettings)
     at MongoDB.Driver.Core.Operations.FindCommandOperation`1.CreateFirstCursorBatch(BsonDocument cursorDocument)
     at MongoDB.Driver.Core.Operations.FindCommandOperation`1.CreateCursor(IChannelSourceHandle channelSource, BsonDocument commandResult)
     at MongoDB.Driver.Core.Operations.FindCommandOperation`1.Execute(RetryableReadContext context, CancellationToken cancellationToken)
     at MongoDB.Driver.Core.Operations.FindOperation`1.Execute(RetryableReadContext context, CancellationToken cancellationToken)
     at MongoDB.Driver.Core.Operations.FindOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
     at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
     at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
     at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, CancellationToken cancellationToken)
     at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
     at MongoDB.Driver.MongoCollectionImpl`1.&lt;&gt;c__DisplayClass45_0`1.&lt;FindSync&gt;b__0(IClientSessionHandle session)
     at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
     at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
     at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
     at MongoDB.Driver.Core.Operations.AsyncCursorSourceEnumerableAdapter`1.GetEnumerator()
     at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
     at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
     at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
  </code></pre>
  <p>I've scoured the Internet in search of an answer.</p>
  <p>I DO NOT WANT TO USE POCO's DECORATION, OR RETURN <strong>BsonDocuments</strong>. Already went this route:</p>
  <pre><code>BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
              BsonSerializer.RegisterSerializer&lt;Guid&gt;(new GuidSerializer(GuidRepresentation.Standard));
  </code></pre>
  <p>and didn't work. Also did the <strong>convention</strong> thing...</p>
  <pre><code>public class GuidAsStringRepresentationConvention : ConventionBase, IMemberMapConvention
      {
          public void Apply(BsonMemberMap memberMap)
          {
              if (memberMap.MemberType == typeof(Guid))
                  memberMap.SetSerializer(new GuidSerializer(BsonType.String));
              
              else if (memberMap.MemberType == typeof(Guid?))
                  memberMap.SetSerializer(new NullableSerializer&lt;Guid&gt;(new GuidSerializer(BsonType.String)));
          }
      }
  </code></pre>
  <p>Didn't work either...</p>
  <p>This is my code:</p>
  <pre><code>public class TestClass
      {
          public Guid Id { get; set; }
          public string Name { get; set; }
          public bool IsActive { get; set; }
      }
  </code></pre>
  <p>This gets called BEFORE the Db connection:</p>
  <pre><code>if (!BsonClassMap.IsClassMapRegistered(typeof(TestClass)))
          {
              BsonClassMap.RegisterClassMap&lt;TestClass&gt;(cm =&gt;
              {
                  cm.MapIdMember(m =&gt; m.Id).SetOrder(0);
                  cm.MapMember(m =&gt; m.Name).SetOrder(1);
                  cm.MapMember(m =&gt; m.IsActive).SetOrder(2);
              });
          }
  
  var conn = configuration.GetConnectionString(&quot;MongoDb&quot;);
              var name = MongoUrl.Create(conn).DatabaseName;
              var client = new MongoClient(conn);
              _db = client.GetDatabase(name); 
  </code></pre>
  <p>after that, I try to get the records, after I initialize the collection in the constructor...</p>
  <pre><code>public async Task&lt;IEnumerable&lt;TestClass&gt;&gt; ReadAllAsync(CancellationToken cancellationToken = default)
          {
              var filter = Builders&lt;TestClass&gt;.Filter.Empty;
              var items = await _collection.Find&lt;TestClass&gt;(filter).ToListAsync(cancellationToken: cancellationToken);
  
              return items;
          }
  </code></pre>
  <p>And of course, I initialize the serializer on &quot;ConfigureServices&quot; in the Startup.cs</p>
  <pre><code>BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
  </code></pre>
  <p>Last, the BSON document with the string GUID (or UUID):</p>
  <pre><code>{ 
      &quot;_id&quot; : &quot;c2ea54fc-3942-4ad5-8315-9e96cc5de034&quot;, 
      &quot;name&quot; : &quot;I'm Going Crazy&quot;, 
      &quot;isActive&quot; : true
  }
  </code></pre>
  <p>If anyone can shed some light, I would appreciate it a lot. Thanks!!!!</p>
  <p><strong>[UPDATE]</strong></p>
  <p>I did this:</p>
  <pre><code>var col = _db.GetCollection&lt;BsonDocument&gt;(&quot;TestCollection&quot;);
  var list = col.Find(FilterDefinition&lt;BsonDocument&gt;.Empty).ToList();
  </code></pre>
  <p>...and I can see the string GUIDs, everything loads perfectly. It's just NOT PARSING correctly. AND I DON'T KNOW WHY. And I refuse to parse this document manually!!!! That's what the driver is supposed to be doing!</p>
  <p><strong>[UPDATE 2]</strong></p>
  <p>Decided to decorate the property with:</p>
  <pre><code>[BsonId]
  [BsonRepresentation(MongoDB.Bson.BsonType.String)]
  public Guid Id { get; set; }
  </code></pre>
  <p>Didn't work either...</p>