Pregunta

I'm trying to run mapreduce on a collection using the C# driver (1.9.0) and a scope variable. I use the following code:

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"));

When I use this code I get the following exception:

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 })

If I remove the scope variable like below it works:

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"));

Does anyone know that's wrong?

¿Fue útil?

Solución

I found a solotion. It turns out I have to use the BsonJavaScriptWithScope instead of 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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top