简体   繁体   English

MapReduce输出C#读取mongoDB

[英]MapReduce output C# reading mongoDB

public class myType
{
public int key1;
public int key2;
public int val1;
public int val2;
public int val3;
};

When I insert myType objects to collection there's no problems with reading. 当我将myType对象插入集合时,阅读没有问题。 Just: 只是:

collection.FindAs<myType>(query);

get objects: 获取对象:

"key1":key1, "key2":key2, "val1":val1, "val2":val2, "val3":val3

But after mapreduce (key is constructed as combination of two fields) output collection has a slightly modified structure. 但是在mapreduce(键构造为两个字段的组合)之后,输出集合的结构稍有修改。 Something like 就像是

"_id" {"key1" : key1, "key2": key2}, "value" : {"val1":val1, "val2":val2, "val3":val3}

What is the way to read object from output collection? 从输出集合读取对象的方法是什么?

Once you have your output collection, you can query it like any other collection. 有了输出集合后,就可以像其他任何集合一样对其进行查询。 However, because the structure is different, you won't be able to use the same C# class to do this. 但是,由于结构不同,您将无法使用相同的C#类来执行此操作。 So, you'll either need to read this as a BsonDocument, or create a couple of new classes like below: 因此,您要么需要将其作为BsonDocument读取,要么创建如下所示的几个新类:

public class Keys
{
  [BsonElement("key1")]
  public int Key1;
  [BsonElement("key2")]
  public int key2;
}

public class values
{
  [BsonElement("val1")]
  public int Val1;
  [BsonElement("val1")]
  public int Val2;
  [BsonElement("val1")]
  public int Val3;
}

public class MapReduceOutput
{
  [BsonId]
  public Keys Keys;
  [BsonElement("values")]
  public Values Values;
}

Then you can do this: 然后,您可以执行以下操作:

collection.FindAs<MapReduceOutput>(query);

Hope this helps. 希望这可以帮助。

Simple for each through the m/r results supposed to work fast (at least faster than automatic deserializtion): 通过m / r结果,每个结果都很简单,应该可以快速工作(至少比自动反序列化更快):

var result = RunMapReduce(..);
foreach (var item in result.InlineResults)
{
   var id = item["_id"].AsBsonDocument;
   var value = item["value"].AsBsonDocument;

   var obj = new myType();
   obj.key1 = id["key1"].AsInt32;
   //and so on
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM