How to filter items in collection with UUID subtybe 3 and 4?

Hi, @Roman_Marusyk,

Welcome to the MongoDB Community Forums. Querying GUIDs with different representations is a bit challenging because the server does a binary comparison of the data, which includes both the binary subtype and actual byte ordering. Thus you can’t ignore the subtype in the comparison because the actual bytes are written in a different order on disk.

If you don’t know whether a particular document is written with a subtype 3 or subtype 4 GUID, you can write a query to match both cases:

#pragma warning disable CS0618 // Type or member is obsolete
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3; // you must be in V3 mode for this to work
#pragma warning restore CS0618 // Type or member is obsolete

var client = new MongoClient();
var db = client.GetDatabase("test");
var coll = db.GetCollection<UserModel>("guids");

var guid = Guid.NewGuid();
var id3 = new BsonBinaryData(guid, GuidRepresentation.CSharpLegacy);
var id4 = new BsonBinaryData(guid, GuidRepresentation.Standard);
var builder = Builders<UserModel>.Filter;
var filter = builder.Or(
        builder.Eq("Id", id3),
        builder.Eq("Id", id4));
var query = coll.Find(filter);
Console.WriteLine(query);

This results in the following query being sent to the server:

find({ "$or" : [{ "_id" : HexData(3, "5cf29873-9067-284c-aab1-c57cfa204327") }, { "_id" : UUID("7398f25c-6790-4c28-aab1-c57cfa204327") }] })

Hope that helps.

Sincerely,
James