2

It appears there is an issue with the count operation on a geospatial query that contains more than 100 results. If I run the following query I still get a count of 100 no matter what.

db.locations.find({"loc":{$nearSphere:[50, 50]}}).limit(1000).count()

I understand that the default size limit on a query that uses the "near" syntax is 100 but it appears you cannot return more than that. Am I doing something wrong or is there a workaround for this?

Travis
  • 336
  • 1
  • 5

3 Answers3

2

Try "within" instead of "near". This works for me,

center = [50, 50];
radius = 1/111.12; //convert it to KM.

db.places.count({"loc" : {"$within" : {"$center" : [center, radius]}}})

I found this at https://jira.mongodb.org/browse/SERVER-856

user644745
  • 5,673
  • 9
  • 54
  • 80
  • Sothe reason seems like $near set a default limit of 100 results.see http://stackoverflow.com/questions/5492901/query-near-vs-within – user890973 Jul 09 '13 at 10:34
0

It is not an issue with the mongo query. Try using cursor.size() rather than cursor.count(). Count does not take into account the limit where size does. So if you use .size() instead of count() you should get a print out of the correct number of returned items.

Check out this stack overflow Q and A for a clear solution to your issue. Difference between cursor.count() and cursor.size() in MongoDB

Community
  • 1
  • 1
miss.serena
  • 1,170
  • 1
  • 11
  • 28
0

For what it's worth, even though the count reported for the cursor is 100 (regardless of what limit you give the query, even "10") you actually get the "limit" number of results back when you run through the query.

Here's a link to an issue where they assert it isn't broken:

Hope that helps!

zentrope
  • 141
  • 3