简体   繁体   English

MongoDb .NET - ObjectId 序列化

[英]MongoDb .NET - ObjectId serialization

I'm using my .net6.0 api, this model:我正在使用我的 .net6.0 api,这个模型:

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"},... {"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"},... {"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.我的愿望是在 json 中有一个字符串表示,在 mongo 实体中保留一个 objectid 列表。

You should change the model like this adding the following attribute:您应该像这样更改模型,添加以下属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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