【问题标题】:What is wrong with this map-reduce query on mongo?mongo 上的这个 map-reduce 查询有什么问题?
【发布时间】:2012-03-27 19:30:31
【问题描述】:

请观察 mongo shell:

> map
function map() {
    if (this.server_location[0] == -77.0367) {
        emit(this._id, this);
    }
}
> reduce
function reduce(key, values) {
    return values[0];
}
> db.static.mapReduce(map, reduce, {out: 'x', query: {client_location:{$near:[-75.5,41.89], $maxDistance: 1}}})
{
        "result" : "x",
        "timeMillis" : 43,
        "counts" : {
                "input" : 100,
                "emit" : 0,
                "reduce" : 0,
                "output" : 0
        },
        "ok" : 1,
}
> db.static.find({client_location:{$near:[-75.5,41.89], $maxDistance: 1}, $where: "this.server_location[0] == -77.0367" }).count()
7
>

我首先运行一个 map-reduce 实现,然后运行一个查询。结果是不同的。我做错了什么?

编辑

我已经像这样更改了地图功能:

function map()
{
    if (this.server_location[0] < -77 && this.server_location[0] > -78)
    {
        print(this._id + ": server_location[0] = " + this.server_location[0]);
    }
    if (this.server_location[0] == -77.0367)
    {
        emit(this._id, this);
    }
}

运行 map-reduce 会在 mongod 控制台上打印以下内容:

1412262185: server_location[0] = -77.8586
1412493418: server_location[0] = -77.8586
1412497409: server_location[0] = -77.8586
1412559515: server_location[0] = -77.8586
1412666474: server_location[0] = -77.6114
1412895269: server_location[0] = -77.6114
1412962473: server_location[0] = -77.6114

另一方面,使用$where 语句的查询会产生以下结果:

/* 0 */
{
  "_id" : 1411941588,
  "server_location" : [-77.0367, 38.8951],
  "client_location" : [-75.6485, 41.4201]
}

/* 1 */
{
  "_id" : 1412382406,
  "server_location" : [-77.0367, 38.8951],
  "client_location" : [-75.728, 41.4486]
}

/* 2 */
{
  "_id" : 1412987742,
  "server_location" : [-77.0367, 38.8951],
  "client_location" : [-75.8962, 41.2808]
}

/* 3 */
{
  "_id" : 1412988363,
  "server_location" : [-77.0367, 38.8951],
  "client_location" : [-75.8962, 41.2808]
}

/* 4 */
{
  "_id" : 1412989085,
  "server_location" : [-77.0367, 38.8951],
  "client_location" : [-75.8962, 41.2808]
}

/* 5 */
{
  "_id" : 1413017856,
  "server_location" : [-77.0367, 38.8951],
  "client_location" : [-75.9534, 41.2973]
}

/* 6 */
{
  "_id" : 1412398078,
  "server_location" : [-77.0367, 38.8951],
  "client_location" : [-76.0341, 41.1838]
}

我不明白为什么在 map-reduce 期间没有找到这些。有人吗?

编辑 2

顺便说一句,当我将 $where 子句的条件更改为 this.server_location[0] == -77.8586 || this.server_location[0] == -77.6114 时,我得到了 50 个结果,而不是 map 函数打印的 7 个结果。奇怪的是,map-reduce 打印的 7 个结果是查询找到的 50 个结果中的前 7 个结果。

我迷路了。

编辑 3

我想我知道问题出在哪里。似乎 map-reduce 只输入了前 100 条记录。问题是下一步是什么?

【问题讨论】:

  • 为虚拟答案道歉。我会加载 mongo 并尝试一下。
  • 如果你尝试 maxdistance 为 3 会怎样?它找到东西了吗?

标签: mongodb mapreduce


【解决方案1】:

知道了:

使用 limit() 指定要返回的最大点数(如果未指定,则应用默认限制 100):

形成geospacial indexing 页面。

问题在于您的 $near 过滤,默认情况下它只返回前 100 个结果。要解决此问题,您必须指定查询的限制。我不确定这是否与 map-reduce 语句兼容。你可以试试:

db.static.mapReduce(...).limit(500)

看看这是否会给你带来不同的结果。

【讨论】:

  • 谢谢,伙计。我用 $within $center 替换了 $near,它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 2017-09-27
  • 2015-07-31
  • 1970-01-01
  • 2011-09-29
  • 2011-10-13
  • 2010-09-07
相关资源
最近更新 更多