在mongo上使用map-reduce查询有什么问题? [英] What is wrong with this map-reduce query on mongo?
问题描述
请观察mongo shell:
Please, observe the 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实现,然后再运行与查询相同的操作.结果是不同的.我在做什么错了?
I am running first a map-reduce implementation and then the same thing as a query. The results are different. What am I doing wrong?
编辑
我已经更改了地图功能,如下所示:
I have changed the map function like this:
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控制台上打印以下内容:
Running the map-reduce prints the following on the mongod console:
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
语句的查询将产生以下结果:
On the other hand, the query with the $where
statement yields the following results:
/* 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]
}
我不明白为什么在地图缩减中找不到这些东西.有人吗?
I do not understand how come these were not found during the map-reduce. Anyone?
编辑2
顺便说一句,当我将 $ where
子句的条件更改为 this.server_location [0] == -77.8586 ||时,this.server_location [0] == -77.6114
我得到50个结果,而不是map函数打印的7个结果.奇怪的是,map-reduce打印的7个结果是查询找到的50个结果中的前7个结果.
BTW, when I change the condition of the $where
clause to this.server_location[0] == -77.8586 || this.server_location[0] == -77.6114
I get back 50 results and not 7 as printed by the map function. It is curious that the 7 results printed by the map-reduce are the first 7 results from the 50 found by the query.
我迷路了.
编辑3
我想我知道出了什么问题.似乎map-reduce仅填充了前100条记录.问题是下一步是什么?
I think I know what is the problem. Seems like map-reduce is fed with only the first 100 records. The question is what next?
推荐答案
知道了:
使用limit()指定要返回的最大点数(如果未指定,则默认限制为100):
Use limit() to specify a maximum number of points to return (a default limit of 100 applies if unspecified):
形成地理空间索引页面.
问题是您的 $ near
过滤,默认情况下,该过滤仅返回前100个结果.为了解决这个问题,您将必须为查询指定一个限制.我不确定这是否与map-reduce语句兼容.您可以尝试:
The problem is your $near
filtering, which by defaults only returns the first 100 results. To solve this, you will have to specify a limit to the query. I'm not sure that this is compatible with the map-reduce statement. You could try:
db.static.mapReduce(...).limit(500)
看看这是否能给您带来不同的结果.
and see if this gives you different results.
这篇关于在mongo上使用map-reduce查询有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!