当前位置: 首页 > 编程语言 > C#

MongoDBC#Driver2.0-HowtoGetResultsfromMapReduceAsync分享

时间:2023-04-10 23:31:34 C#

MongoDBC#Driver2.0:HowtoGetResultsfromMapReduceAsyncMongoDBC#Driver2.0:HowtoGetResultsfromMapReduceAsyncI'musingMongoDBversion3,C#驱动程序2.0,并将获取MapReduceAsync方法的结果。我有这个集合"Users":{"_id":1,"firstName":"Rich","age":"18"}{"_id":2,"firstName":"Rob","age":"25"}{"_id":3,"firstName":"Sarah","age":"12"}VisualStudio中的代码:varmap=newBsonJavaScript(@"varmap=function(){emit(NumberInt(1),这个年龄);};");varreduce=newBsonJavaScript(@"varreduce=function(key,values){varsum=0;values.forEach(function(item){sum+=NumberInt(item);});returnsum;};");varcoll=db.GetCollection("用户");varoptions=newMapReduceOptions();//TResult应该是什么?options.OutputOptions=MapReduceOutputOptions.Inline;varres=coll.MapReduceAsync(map,reduce,options).Result.ToListAsync();//获取res的值...//或者如果结果是一个列表...foreach(variteminres){//获取值并做一些事情...}TResult可以是一个BsonDocument或表示类型减少项目结果的特定类。我认为对于你的例子,你可以有一个像这样的通用类:publicclassSimpleReduceResult{publicstringId{get;放;}公共T值{得到;放;C#Driver2.0:如何从MapReduceAsync获取结果分享的所有内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——varoptions=newMapReduceOptions>();本文来自网络收藏,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: