简体   繁体   English

MongoDB C#驱动程序2.0:如何从MapReduceAsync获取结果

[英]MongoDB C# driver 2.0: How to get the result from MapReduceAsync

MongoDB C# driver 2.0: How to get the result from MapReduceAsync MongoDB C#驱动程序2.0:如何从MapReduceAsync获取结果

I'm using MongoDB version 3, C# driver 2.0 and would get the result of MapReduceAsync method. 我正在使用MongoDB版本3,C#驱动程序2.0,并将获得MapReduceAsync方法的结果。 I have this collection "users": 我有这个集合“用户”:

{ "_id" : 1, "firstName" : "Rich", "age" : "18" }
{ "_id" : 2, "firstName" : "Rob", "age" : "25" }
{ "_id" : 3, "firstName" : "Sarah", "age" : "12" }

The code in VisualStudio: VisualStudio中的代码:

var map = new BsonJavaScript( @"
    var map = function()
    {
        emit(NumberInt(1), this.age);
    };");

var reduce =  new BsonJavaScript(@"
    var reduce = function(key, values)
    {
        var sum = 0;

        values.forEach(function(item)
        {
            sum += NumberInt(item);
        });

        return sum;
    };");

var coll = db.GetCollection<BsonDocument>("users");
var options = new MapReduceOptions<BsonDocument, TResult>();//what should be TResult?

options.OutputOptions = MapReduceOutputOptions.Inline;

var res = coll.MapReduceAsync(map, reduce, options).Result.ToListAsync();

//get the values of res...

//or if the result is a list...
foreach(var item in res)
{
    //get the values and do something...
}

TResult can be a BsonDocument or a specific class which represent the result of type reduce item. TResult可以是BsonDocument或表示类型reduce项的结果的特定类。

I think for your example, you could have a generic class like this : 我想对于你的例子,你可以有一个像这样的泛型类:

public class SimpleReduceResult<T>
{
    public string Id { get; set; }

    public T value { get; set; }
}

And your options declaration would be 你的选择声明就是

var options = new MapReduceOptions<BsonDocument, SimpleReduceResult<int>>();

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

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