c# - MongoDB C# 驱动程序 2.0 : How to get the result from MapReduceAsync

标签 c# mongodb asynchronous mapreduce mongodb-csharp-2.0

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

最佳答案

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>>();

关于c# - MongoDB C# 驱动程序 2.0 : How to get the result from MapReduceAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776621/

相关文章:

c# - 使用静态字典时,我在 ASP.NET 中收到错误 "Item has already been added."

mongodb - 如何过滤子数组但保留根内容?

node.js - 索环:类型错误:无法读取未定义的属性 'modelSchemas'

java - Spring 异步方法不适用于 EndPoint

c# - 异步等待新线程的行为

c# - 使用 1bpp 图像

c# - HttpContent:已添加具有相同键的项目

c# - Lambda 表达式中的多个 Where 子句

mysql - 现代 Web 应用程序如何对大量快速变化的数据实现缓存和数据持久化?

c# - 异步仍然卡住 ui