gpt4 book ai didi

c# - ObjectId 数组的 MongoDB C# BsonRepresentation

转载 作者:可可西里 更新时间:2023-11-01 10:06:23 26 4
gpt4 key购买 nike

我有这样的文档方案:

{
"_id" : ObjectId("4fbb728d80db260988580e05"),
"titleFull" : "Foo, Inc",
"titleShort" : "Foo",
"countries" : [
ObjectId("4fba04ef80db260988f8b607"),
ObjectId("4fba05f880db260988cd5cfd") ],
"type" : "company"
}

ASP.NET MVC 4 Web API 项目中的此类:

public class Company
{
[BsonRepresentation(BsonType.ObjectId)]
public String id { get; set; }
public String titleFull { get; set; }
public String titleShort { get; set; }
//[BsonRepresentation(BsonType.ObjectId)]
//public String[] countries { get; set; } — not working
public ObjectId[] countries { get; set; }
public String type { get; set; }
}

当我在 /api/countries 上发送 GET 请求时,我收到 JSON 文档(它是 mvc 反序列化):

{
"id": "4fba097e80db2609886ce7f2",
"titleFull": "Foo, LLC",
"titleShort": "Foo",
"countries": [
{
"_increment": 16299527
"_machine": 8444710
"_pid": 2440
"_timestamp": 1337591023
},
{
"_increment": 13458685
"_machine": 8444710
"_pid": 2440
"_timestamp": 1337591288
}
],
"type": "company"
}

有没有办法像这样做 JSON 响应:

{
"id": "4fba097e80db2609886ce7f2",
"titleFull": "Foo, LLC",
"titleShort": "Foo",
"countries": ["4fba04ef80db260988f8b607","4fba05f880db260988cd5cfd"],
"type": "company"
}

最佳答案

future 读者须知

Rober Stam 在谷歌群组中写道:

There is a bug in the deserialization code. In the case of an array the [BsonRepresentation] attribute is in fact applied to the items and not the array.

I've created a JIRA ticket for this:

https://jira.mongodb.org/browse/CSHARP-479

因此,如果您有同样的问题,请跟踪此工单。

关于c# - ObjectId 数组的 MongoDB C# BsonRepresentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10781480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com