MongoDB C# filter with UUID (GUID) is returning 0 result

Viewed 973

I have a MongoDB collection "Posts" with data like this:

{
 "_id": UUID('5fd671be-df7c-4dd2-87dc-9877a7fb1953'),
 "EditorId": UUID('f79fc0d8-0200-4eef-91c2-4062ee9a2354'),
"Name": "Name 01"
}

In C# I tried to get all the record for a specific EditorId, I tried this:

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;

var postsCollection = _database.GetCollection<BsonDocument>("Posts");
var postsCollection2 = _database.GetCollection<Post>("Posts");
 
Guid editorId = ...
 
var postsDocs = postsCollection.Find(Builders<BsonDocument>.Filter.Eq("EditorId", editorObj.Id)).ToList();
var posts = postsCollection2.Find(p => p.EditorId == editorObj.Id).ToList();

But none of this way work, I got no result! Do you have any idea about the problem and how to solve this?

Thx

1 Answers

I have find a solution, I post the solution, hope it will help someone.

The trick is to add

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;

BEFORE doing the database connection, I was doing this just after!

Related