c# - ObjectId 数组的 MongoDB C# BsonRepresentation

标签 c# mongodb asp.net-mvc-4 asp.net-web-api mongodb-.net-driver

我有这样的文档方案:

{
  "_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/

相关文章:

java - Morphia:使用已弃用的配置选项对集合进行索引(复合键索引)

Asp.net POST 参数始终为空

asp.net-mvc-4 - 模拟 WebSecurity 提供者

c# - 使用网络响应加载网络浏览器

c# - 有没有一种技术可以将 DebugFormat() 与构建起来很昂贵的参数一起使用?

c# - WebClient.DownloadString() 结果在某些网站上存在编码问题!使用波斯语(波斯语)

c# - Queue<T> 线程安全 : one writer, 一个读者

javascript - 不使用 $project 将嵌套文档投影到根级别

mongodb - 按数组元素比较查找

asp.net-mvc - Dotless + MVC + Azure 有垃圾而不是 CSS