MongoDB的C#2.0的驱动程序:如何从马preduceAsync结果 [英] MongoDB C# driver 2.0: How to get the result from MapReduceAsync
本文介绍了MongoDB的C#2.0的驱动程序:如何从马preduceAsync结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MongoDB的C#2.0的驱动程序:如何从马preduceAsync结果
我使用MongoDB的版本3,C#2.0的驱动程序,并会得到马preduceAsync方法的结果。
我有这个集用户:
{_id:1,名字:富,时代:18}
{_id:2,名字:抢,时代:25}
{_id:3,名字:莎拉,时代:12}
在code在VisualStudio的:
VAR地图=新BsonJavaScript(@
VAR地图=功能()
{
发射(NumberInt(1),this.age);
};);VAR减少=新BsonJavaScript(@
VAR减少=功能(键,值)
{
变种总和= 0; values.forEach(函数(项目)
{
总和+ = NumberInt(项目);
}); 返回总和;
};);VAR科尔= db.GetCollection< BsonDocument>(用户);
VAR的选择=新马preduceOptions< BsonDocument,TResult>(); //应该怎样TResult?options.OutputOptions =麻preduceOutputOptions.Inline;VAR解析度= coll.Ma preduceAsync(地图,缩小,期权).Result.ToListAsync();//获取资源的价值...//或如果结果是一个列表...
的foreach(在res VAR项)
{
//获取价值观和做一些事情...
}
解决方案
TResult可以是BsonDocument或重新present型的结果,减少项目特定类。
我觉得你的例子,你可以有这样的泛型类:
公共类SimpleReduceResult< T>
{
公共字符串ID {搞定;组; } 公众的T值{搞定;组; }
}
和您选择的声明将是
VAR选项=新马preduceOptions< BsonDocument,SimpleReduceResult< INT>>();
MongoDB C# driver 2.0: How to get the result from MapReduceAsync
I'm using MongoDB version 3, C# driver 2.0 and would get the result of MapReduceAsync method. 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:
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.
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>>();
这篇关于MongoDB的C#2.0的驱动程序:如何从马preduceAsync结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文