简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM