Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MongoDb .NET – ObjectId serialization

I’m using my .net6.0 api,
this model:

public class Tournament
{
    public List<ObjectId> FriendsId { get; set; }

is serialized as:

{"FriendsId":[{"timestamp":1654717093,"machine":540378,"pid":-19594,"increment":6387934,"creationTime":"2022-06-08T19:38:13Z"},…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but I want to have a string representation like this:

{"FriendsId":[{"62a0f94f185b87a1a88c2354"},…

what is the right way to obtain this result?
my wish is to have a string rapresentation in json, keeping a list of objectid in the mongo entity.

>Solution :

You should change the model like this adding the following attribute:

[BsonRepresentation(BsonType.ObjectId)]
public List<string> Friends { get; set; } = new  List<string>();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading