【问题标题】:Unable to set query filter in a mongodb mapReduce command无法在 mongodb mapReduce 命令中设置查询过滤器
【发布时间】:2012-11-11 17:43:24
【问题描述】:

我正在尝试使用查询过滤 mapReduce 命令。 mapReduce 命令似乎没有使用此查询。当我使用具有相同参数的 runCommand 时,会使用查询过滤器。我尝试使用 mongodb 2.2.1 和 2.0.1。

我的mapReduce函数的查询没有使用。

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

此命令不起作用:

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

此命令有效:

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
}

使用 runCommand 可以使用查询。有什么想法吗?

【问题讨论】:

    标签: mongodb mapreduce


    【解决方案1】:

    您需要将outquery 选项组合成一个对象:

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

    【讨论】:

    • 谢谢!你救了我的星期天!
    猜你喜欢
    • 1970-01-01
    • 2021-03-19
    • 2019-11-24
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    相关资源
    最近更新 更多