3

MongoDB C# ドライバー 2.0: MapReduceAsync から結果を取得する方法

私は MongoDB バージョン 3、C# ドライバー 2.0 を使用しており、MapReduceAsync メソッドの結果を取得します。私はこのコレクション「ユーザー」を持っています:

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

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...
}
4

1 に答える 1

1

TResult は、BsonDocument または型 reduce item の結果を表す特定のクラスにすることができます。

あなたの例では、次のようなジェネリッククラスを持つことができると思います:

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

    public T value { get; set; }
}

そして、あなたのオプション宣言は

var options = new MapReduceOptions<BsonDocument, SimpleReduceResult<int>>();
于 2015-10-21T14:52:19.713 に答える