我正在尝试使用 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"));
当我使用这段代码时,出现以下异常:
An exception of type 'MongoDB.Driver.MongoCommandException' occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: Command 'mapreduce' failed: exception: Can't canonicalize query {} (response: { "errmsg" : "exception: Can't canonicalize query {}", "code" : 17238, "ok" : 0.0 })
如果我像下面那样删除范围变量,它会起作用:
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();
关于c# - MongoDB - 无法规范化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23558258/