MongoDB - 在 C# 中映射 map-reduce 集合

发布于 2024-11-17 05:46:50 字数 934 浏览 2 评论 0原文

我正在运行一个地图缩减作业,将结果数据转储到一个集合中,“产品”集合中的元素如下所示(该结构是由 Mongo 生成的,我不知道它是否可以控制):

{
    "_id" : { "ProductId" : "1:000001", "ProductTitle" : "Some product with ID 1:000001" }, 
    "value" : { "TotalImpressions" : 3, "TotalClicks" : 40 } 
}

理想情况下,我想要将每个条目映射到以下平面对象:

public class Product 
{
    public string ProductId { get; set; }
    public string ProductTitle { get; set; }
    public int TotalImpressions { get; set; }
}

这显然不起作用,因为序列化程序在根级别查找属性“id”和“value”,而这些属性在该类中不存在。我采取的解决方法是在对象出现时对其进行建模,例如:

public class ProductRow
{
    /* implementation of these objects excluded, but they just reflect the json objects */
    public ProductIdentifier Id { get; set; }
    public Product value { get; set; }
}

哪个映射很好,但是有点冗长,我宁愿避免拥有所有这些额外的对象。

是否可以配置 BSON 解串器来支持此映射?我浏览了文档,但没有看到任何明显的解决方案。

注意:我的工作环境仅限于 .NET 3.5,因此在考虑答案时请记住这一点。

I am running a map reduce job that dumps the resulting data into a collection, the elements in the "products" collection look like this (the structure is generated by Mongo and I'm not aware if it can be controlled):

{
    "_id" : { "ProductId" : "1:000001", "ProductTitle" : "Some product with ID 1:000001" }, 
    "value" : { "TotalImpressions" : 3, "TotalClicks" : 40 } 
}

Ideally, I want to map each entry to the following flat object:

public class Product 
{
    public string ProductId { get; set; }
    public string ProductTitle { get; set; }
    public int TotalImpressions { get; set; }
}

This obviously doesn't work as the serializer looks for properties "id" and "value" at the root level, which don't exist on that class. The workaround I have in place is to model the object as they appear, e.g.:

public class ProductRow
{
    /* implementation of these objects excluded, but they just reflect the json objects */
    public ProductIdentifier Id { get; set; }
    public Product value { get; set; }
}

Which maps fine, however it's a little verbose and I'd rather avoid having all those extra objects.

Is it possible to configure the BSON deserializer to support this mapping? I've had a look through the documentation, but haven't seen any obvious solution.

NB: I am restricted by working environment to .NET 3.5, so please bear that in mind when considering an answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-11-24 05:46:50

您可以轻松地自己进行反序列化(如@Avish建议的那样),所以这里是您的案例的完整示例:

var mongoServer = MongoServer.Create("mongodb://localhost:27020");
var database = mongoServer.GetDatabase("StackoverflowExamples");
var products = database.GetCollection("products");

var result = new Product();

var item = products.FindOne();
var id = item["_id"].ToBsonDocument();
var value = item["value"].ToBsonDocument();

result.ProductId = id["ProductId"].AsString;
result.ProductTitle = id["ProductTitle"].AsString;
result.TotalImpressions = value["TotalImpressions"].AsInt32;

You can easy do deserialization yourself(as @Avish suggested), so here is complete example for your case:

var mongoServer = MongoServer.Create("mongodb://localhost:27020");
var database = mongoServer.GetDatabase("StackoverflowExamples");
var products = database.GetCollection("products");

var result = new Product();

var item = products.FindOne();
var id = item["_id"].ToBsonDocument();
var value = item["value"].ToBsonDocument();

result.ProductId = id["ProductId"].AsString;
result.ProductTitle = id["ProductTitle"].AsString;
result.TotalImpressions = value["TotalImpressions"].AsInt32;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文