mark
mark

Reputation: 62886

What is wrong with this map-reduce query on mongo?

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
>

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?

EDIT

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

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

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?

EDIT 2

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.

I am lost.

EDIT 3

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?

Upvotes: 2

Views: 717

Answers (1)

brice
brice

Reputation: 25039

Got it:

Use limit() to specify a maximum number of points to return (a default limit of 100 applies if unspecified):

Form the geospacial indexing page.

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.

Upvotes: 4

Related Questions