我试图在我的应用程序中运行MapReduce查询。我已经创建了相关的JS,并通过Mongo-Shell对其进行了验证:
var map = function() {
if (this.class_artist != null)
for (var i = 0; i < this.class_artist.length; i++) {
var key = {
artist: this.class_artist[i],
language: this.language.substring(0,2),
};
var value = {
count: 1
};
emit(key, value);
}
}
var reduce = function(key, value) {
var sum = 0;
value.forEach(function(value) {
sum += value['count'];
});
return {count: sum};
}
db.articles.mapReduce(map, reduce, {out: {inline: 1}})
我有以下c#翻译:
var articleCollection = database.GetCollection<Article_Base>("articles");
string map = @"var map = function() {
if (this.class_artist != null)
for (var i = 0; i < this.class_artist.length; i++) {
var key = {
artist: this.class_artist[i],
language: this.language.substring(0,2),
};
var value = {
count: 1
};
emit(key, value);
}
}";
string reduce = @"var reduce = function(key, value) {
var sum = 0;
value.forEach(function(value) {
sum += value['count'];
});
return {count: sum};
}";
var options = new MapReduceOptionsBuilder();
options.SetOutput(MapReduceOutput.Inline);
var result = articleCollection.MapReduce(map, reduce, options).GetResults();
然而,即使我使用相同的地图&amp;减少函数,c#代码没有返回任何结果。
我是mapReduce的新手,尽管阅读了文档,我还不确定如何继续。
非常感谢任何帮助!
答案 0 :(得分:1)
您的翻译过于文字,您只需要将函数定义为字符串:
string map = @"function() {" // and the rest of it
然后mapReduce方法有待处理