我正在尝试使用c#驱动程序(1.9.0)和作用域变量在集合上运行mapreduce。我使用以下代码:
var map = @"function() {
emit(this._id, foo);
};";
var reduce = @"function(key, values) {
return values;
};";
var options = new MapReduceOptionsBuilder();
options.SetOutput(MapReduceOutput.Inline);
options.SetScope(new ScopeDocument("foo", "foo"));
当我使用这段代码时,我得到了以下异常:
类型为'MongoDB.Driver '的异常。MongoCommandException的发生在MongoDB.Driver.dll中,但在用户代码中没有处理
附加信息:命令'mapreduce' failed: exception: Can't规范化查询{}(response: {"errmsg": "exception: Can't规范化查询{}","code": 17238, "ok": 0.0})
如果我像下面这样删除scope变量,它就可以工作了:
var map = @"function() {
//emit(this._id, foo);
emit(this._id, 1);
};";
var reduce = @"function(key, values) {
return values;
};";
var options = new MapReduceOptionsBuilder();
options.SetOutput(MapReduceOutput.Inline);
//options.SetScope(new ScopeDocument("foo", "foo"));
有人知道这是错的吗?
我找到了一个解决方案。事实证明,我必须使用BsonJavaScriptWithScope而不是SetScope。
var map = @"function() {
emit(this._id, foo);
};";
var reduce = @"function(key, values) {
return values;
};";
var scope = new BsonDocument("foo", "foo");
var args = new MapReduceArgs()
{
MapFunction = new BsonJavaScriptWithScope(map, scope),
ReduceFunction = new BsonJavaScript(reduce),
OutputMode = MapReduceOutputMode.Inline
};
var results = collection.MapReduce(args).GetResults();