请观察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
BTW,当我将$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条记录。问题是下一步是什么?
答案 0 :(得分:4)
知道了:
使用limit()指定要返回的最大点数(如果未指定则应用默认限制100):
形成geospacial indexing页面。
问题是您的$near
过滤,默认情况下只返回前100个结果。要解决此问题,您必须为查询指定限制。我不确定这是否与map-reduce语句兼容。你可以尝试:
db.static.mapReduce(...).limit(500)
并查看这是否会给您带来不同的结果。