c# - 如何将嵌套的 Document 反序列化为特定的类对象?

标签 c# mongodb mongodb-.net-driver

我是 MongoDb 的新手,我正在尝试将嵌套在父文档中的文档数组序列化为特定的类对象。

我的文档对象如下所示:

Project
{
    "_id" : ObjectId("589da20997b2ae4608c99173"),
    // ... additional fields
    "Products" : [ 
        { 
            "_id" : ObjectId("56971340f3a4990d2c199853"),
            "ProductId" : null,
            "Name" : null,
            "Description" : null,
            // ... quite a few more properties
            "Properties" : null 
        }
    ],
    "CreatorUserId" : LUUID("af955555-5555-5555-5555-f1fc4bb7cfae")
}

所以基本上它是一个名为 Project 的文档,其中包含一个嵌套的 Product 文档数组。

以下是当前类的样子:

public class Project : BaseClasses {
    public ObjectId ParentCreatorUserIdProject { get; set; }  
    // ... additional Fields 
    public IEnumerable<IProjectItem> ProjectItems { get; set; } 
    //public IEnumerable<IProductDetails> Products { get; set; }
}

[DataContract][Serializable]
[BsonIgnoreExtraElements][MongoCollection("products")]
public class Product {
    public string Name { get; set; } 
    public string Description { get; set; 
    // .. a bunch of Fields
} 

当前行为

当前 Project 文档返回一个序列化的 Product 数组。

期望的行为

我希望文档返回一个序列化的 ProjectItems 数组。 ProjectItems 类很简单:

[Serializable]
[BsonIgnoreExtraElements]
public class ProjectItem {
    public string Name { get; set; } 
    public string Description { get; set; 
    // .. a handful of Fields
}

我的尝试

我曾尝试使用 BsonClassMap 创建一个映射,但是,尽管阅读了 entire document on mapping,我对此类类型的了解还不够多。 .我应该注意,我已经定义了这个类映射与以前的开发人员的其他映射,并且确信(单元测试)他们执行了。

  BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
      cm.AutoMap();
      cm.SetDiscriminator("ProjectItem");
      cm.SetDiscriminatorIsRequired(true);
  });

不幸的是,文档仍然被序列化为 Product 数组。

我的问题

如何将这个嵌套的 Products 数组序列化为 ProjectItem 类的数组?

最佳答案

所以答案非常简单...无论您将其存储为哪种类型,您都希望将其设置为鉴别器。

  BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
      cm.AutoMap();
      cm.SetDiscriminator("Product"); // <--- it was stored as a Product
      cm.SetDiscriminatorIsRequired(true);
  });

让我们为我的 MongoDb 新手看另一个例子...如果您的 Product 最初存储为 ProductDetails 您的对象将有一个 _t 在数据库中。 _t 告诉 MongoDb 它存储为什么类型。

所以你的对象看起来像这样:

"Products" : [ 
    { 
         "_t" : "ProductDetails",
         "_id" : ObjectId("56971340f3a4990d2c199853"),
         "ProductId" : null,
         "Name" : null,
         "Description" : null,
         // ... quite a few more properties
         "Properties" : null 
    }
]

由于我们的 Product 存储有鉴别器类型“ProductDetail”,因此我们将其序列化为 ProjectItem,如下所示:

  BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
      cm.AutoMap();
      cm.SetDiscriminator("ProductDetails"); // <--- it was stored as a ProductDetails
      cm.SetDiscriminatorIsRequired(true);
  });

关于c# - 如何将嵌套的 Document 反序列化为特定的类对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43162009/

相关文章:

c# - 如何修改Shell中的图片以显示通知徽章?

c# - VS 2010 的功能

c# - MySQL IN 子句 : How can I get multiple rows when I use a same value multiple times?

mongodb - MongoDB UUID Standard 而不是 Legacy 有什么性能优势?

MongoDB 重命名收集失败,返回 "exceeds maximum length of 32, allowing for index names"

c# - 使用反射从 COM 对象调用重载方法?

MongoDB:当我使用 mongoimport --upsert 时如何加快每秒插入速度

json - 包含空值的 BsonDocument 的 Newtonsoft.Json.JsonConvert.SerializeObject 因 InvalidCastException 而失败

php - 使用日期范围和另一个条件的 MongoDB 搜索

c# - MongoDb C# - 如何在文档中存储 GeoJSON