請遵守蒙戈外殼:mongo上的map-reduce查詢有什麼問題?
> 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);
}
}
運行地圖,減少打印的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個結果,而不是7由地圖功能打印。令人好奇的是map-reduce打印的7個結果是來自查詢找到的50個結果中的前7個結果。
我迷路了。
編輯3
我想我知道是什麼問題。看起來像map-reduce只有前100條記錄。接下來的問題是什麼?
對於虛擬答案的道歉。我會加載Mongo並試一試。 – brice 2012-03-27 19:48:14
請做。謝謝。 – mark 2012-03-28 07:22:24
如果您嘗試3的最大距離,該怎麼辦?它能找到這些東西嗎? – Sammaye 2012-03-28 08:20:29