Unable to set query filter in a mongodb mapReduce command

mapreduce, mongodb

Solution

You need to combine the `out` and `query` options into a single object:

res = db.movies.mapReduce(m,r, {out: { inline : 1}, query: {kinds: 'Action'} });

Problem

I am trying to filter a mapReduce command with a query. This query seems to not beeing used by the mapReduce command. When I use the runCommand with the same parameters the query filter is used. I tryed with a mongodb 2.2.1 and a 2.0.1. The query of my mapReduce function is not used. ``` m = function () { if (this.duration > 0) { emit("dur", this.duration); } } r = function (key, values) { var index = 0; var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; index++; } return sum / index; } ``` This command doesn't work : ``` res = db.movies.mapReduce(m,r, {out: { inline : 1}},{query:{kinds:'Action'}}); { "results" : [ { "_id" : "dur", "value" : 5148.227224559308 } ], "timeMillis" : 1849, "counts" : { "input" : 105472, "emit" : 69602, "reduce" : 106, "output" : 1 }, "ok" : 1, } ``` This command work : ``` res = db.runCommand({mapReduce : "movies", map : m, reduce : r, query : {kinds:'Action'}, out : {inline:1} }) { "results" : [ { "_id" : "dur", "value" : 6134.118191572414 } ], "timeMillis" : 238, "counts" : { "input" : 3577, "emit" : 2910, "reduce" : 4, "output" : 1 }, "ok" : 1 } ``` With runCommand the query is used. Any ideas ?

Original source