Home > Mobile >  MongoDb .NET - ObjectId serialization
MongoDb .NET - ObjectId serialization

Time:06-09

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"},...

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.

CodePudding user response:

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

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