Storing uuid in _id using .net driver in c#

ykl
ykl
318 Points
16 Posts

I'm trying to store _id as uuid in mongodb but it's converting to LUUID. Entity model:

[BsonIgnoreExtraElements]
public class myEntity{
        [BsonId]
        public Guid _id { get; set; }
        public string modifier { get; set; }
        public string creator { get; set; }
}

In mongoDB:

{
    "_id" : LUUID("D6C74FF6-DF5D-E74C-B315-00E3DF0B845C"),
    "modifier" : null,
}

I'm using following to generate GUID:

Guid.NewGuid();
Views: 363
Total Answered: 2
Total Marked As Answer: 1
Posted On: 08-Oct-2023 22:43

Share:   fb twitter linkedin
I'm refering here: https://studio3t.com/knowledge-base/articles/mongodb-best-practices-uuid-data/
 - ykl  08-Oct-2023 22:53
Answers
Smith
Smith
2890 Points
78 Posts
         

Try to use serializer option:

 [BsonGuidRepresentation(GuidRepresentation.Standard)]

as:

[BsonIgnoreExtraElements]
public class myEntity{
        [BsonId]
        [BsonGuidRepresentation(GuidRepresentation.Standard)]
        public Guid _id { get; set; }
        public string modifier { get; set; }
        public string creator { get; set; }
}
Posted On: 09-Oct-2023 02:30
Thanks
 - ykl  16-Oct-2023 22:45
ykl
ykl
318 Points
16 Posts
         

Thanks. I resolved issue by adding folloinwg serializer:

MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
Posted On: 16-Oct-2023 22:44
Thanks for sharing solution.
 - Brian  17-Oct-2023 21:37
 Log In to Chat