How do I find a mongo document by GUID? The c# POCO has an Id property that is stored in the mongo database.
The class map for the POCO is registered and called before a client is configured:
BsonClassMap.RegisterClassMap<MyClass>(cm =>
{
cm.AutoMap();
cm.MapIdProperty(m => m.MyId)
.SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
});
Now a document is stored in the database correctly by the looks of it:

However, retrieving the document using a builder yields no result:
Guid myGuidId = ...;
var filterBuilder = Builders<MyClass>.Filter;
var filter = filterBuilder.Eq(d => d.MyId, myGuidId);
return Collection.Find(filter).FirstOrDefaultAsync();
The odd thing is that this query retrieves documents when doing either of these from the start:
- Serialise as string
cm.MapIdProperty(m => m.SimulationId).SetSerializer(new GuidSerializer(BsonType.String)) - Using the obsolete global declaration for GUID serialisation
BsonDefaults.GuidRepresentation = GuidRepresentation.Standard
This behavior is related to
guid representation modeconfigured by default (V2). You can read about this here or in the question hereTo work with your code, apply configuration as below: