MongoDB - 在 C# 中映射 map-reduce 集合
我正在运行一个地图缩减作业,将结果数据转储到一个集合中,“产品”集合中的元素如下所示(该结构是由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以轻松地自己进行反序列化(如@Avish建议的那样),所以这里是您的案例的完整示例:
You can easy do deserialization yourself(as @Avish suggested), so here is complete example for your case: