问题
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?
回答1:
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
回答2:
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:
- https://jira.mongodb.org/browse/SERVER-739
Hope that helps!
回答3:
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
来源:https://stackoverflow.com/questions/6542604/mongodb-geospatial-query-count-issue-always-100