$near queries don't seem to respect limit greater than 100

21 views
Skip to first unread message

Jesse Clark

unread,
Nov 16, 2011, 4:48:31 PM11/16/11
to mongodb-user
I have a collection of studies that has an array field of locations
which contain geo data in the longlat field ( e.g. "longlat" :
[174.792435, -41.246632]). I have a geo index on the studies
collection: { "locations.longlat" : "2d" }.

I have 1460 documents in the studies collection so I would expect this
query to return all of the documents:

> db.studies.find( { 'locations.longlat' : { $near : [-122.276474, 47.54158], $maxDistance : 180 } } ).limit(1500);

but:

> db.studies.find( { 'locations.longlat' : { $near : [-122.276474, 47.54158], $maxDistance : 1000 } } ).limit(1500).toArray().length;
99

Note I am using toArray().length because according to this:
https://jira.mongodb.org/browse/SERVER-2997 count() doesn't work with
$near queries in the shell.

All of my longlat coordinates are between [-180, 180] and I've
verified that all my studies have at least 1 location and all the
locations have values for longlat.

Am I doing something wrong or should I open a Jira ticket for this?

Thanks,
-Jesse


Greg Studer

unread,
Nov 16, 2011, 6:34:16 PM11/16/11
to mongodb-user
Which version of mongodb are you using? Can you run the query
using .explain(true)?

On Nov 16, 4:48 pm, Jesse Clark <je...@jesseclark.com> wrote:
> I have a collection of studies that has an array field of locations
> which contain geo data in the longlat field  ( e.g. "longlat" :
> [174.792435, -41.246632]). I have a geo index on the studies
> collection: { "locations.longlat" : "2d" }.
>
> I have 1460 documents in the studies collection so I would expect this
> query to return all of the documents:
>
> > db.studies.find( { 'locations.longlat' : { $near : [-122.276474, 47.54158], $maxDistance : 180  } } ).limit(1500);
>
> but:
>
> > db.studies.find( { 'locations.longlat' : { $near : [-122.276474, 47.54158], $maxDistance : 1000  } } ).limit(1500).toArray().length;
>
> 99
>
> Note I am using toArray().length because according to this:https://jira.mongodb.org/browse/SERVER-2997count() doesn't work with

Jesse Clark

unread,
Nov 16, 2011, 7:15:54 PM11/16/11
to mongodb-user
I'm on 2.0.1.

> db.studies.find( { 'locations.longlat' : { $near : [-122.276474, 47.54158], $maxDistance : 360 } } ).limit(1500).explain(true);
{
"cursor" : "GeoSearchCursor",
"nscanned" : 1500,
"nscannedObjects" : 1500,
"n" : 1500,
"millis" : 607,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {

},
"allPlans" : [
{
"cursor" : "GeoSearchCursor",
"indexBounds" : {

}
}
],
"oldPlan" : {
"cursor" : "GeoSearchCursor",
"indexBounds" : {

Greg Studer

unread,
Nov 17, 2011, 1:32:17 PM11/17/11
to mongodb-user
Hmm... can't reproduce this... Are there any log messages in the
mongod or mongos?

Jesse Clark

unread,
Nov 17, 2011, 6:02:58 PM11/17/11
to mongodb-user
I don't see any errors related to those queries in the mongos logs.
Only this line:
Wed Nov 16 13:09:15 [conn13] query corengi-development.studies
ntoreturn:1500 nscanned:99 nreturned:99 reslen:4201978 1024ms

Also, if I convert this query from a $near query to a $within query I
can get the full result set.

Greg Studer

unread,
Nov 17, 2011, 6:35:16 PM11/17/11
to mongodb-user
Could you open a JIRA ticket for this? Behavior seems strange - think
the limit is getting passed correctly though. If possible, it would
be good to try a 2.1 nightly mongod with the same data, but not sure
how hard this would be on your end.

Eliot Horowitz

unread,
Nov 18, 2011, 1:40:27 AM11/18/11
to mongod...@googlegroups.com
The problem is that $near can't use cursors yet - so result sets are
limited to 4mb.
99 results happen to be what fits in for you.
See reslen above.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages