From b64f00c581a8b8db17a8f42ad495c3d0b08dc619 Mon Sep 17 00:00:00 2001 From: Manuel Trezza Date: Wed, 13 Jun 2018 12:26:10 +0200 Subject: [PATCH 1/2] added mongoDB performance caveat when using near and sorting constraint --- _includes/js/geopoints.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/_includes/js/geopoints.md b/_includes/js/geopoints.md index d80b56326..153a870bf 100644 --- a/_includes/js/geopoints.md +++ b/_includes/js/geopoints.md @@ -95,4 +95,5 @@ At the moment there are a couple of things to watch out for: 1. Each Parse.Object class may only have one key with a Parse.GeoPoint object. 2. Using the `near` constraint will also limit results to within 100 miles. -3. Points should not equal or exceed the extreme ends of the ranges. Latitude should not be -90.0 or 90.0. Longitude should not be -180.0 or 180.0. Attempting to set latitude or longitude out of bounds will cause an error. +3. Using the `near` constraint combined with an `ascending` or `descending` constraint is not recommended due to performance. This is because `$near` sorts objects by distance and an additional sort constraint re-orders the matching objects, effectively overriding the sort operation already performed. +4. Points should not equal or exceed the extreme ends of the ranges. Latitude should not be -90.0 or 90.0. Longitude should not be -180.0 or 180.0. Attempting to set latitude or longitude out of bounds will cause an error. From a3fa3305bc15531d3048202a5d2626c9d9ca2a20 Mon Sep 17 00:00:00 2001 From: Manuel Trezza Date: Wed, 13 Jun 2018 12:40:58 +0200 Subject: [PATCH 2/2] added examples for sorted parameter --- _includes/js/geopoints.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/_includes/js/geopoints.md b/_includes/js/geopoints.md index 153a870bf..173cc6efb 100644 --- a/_includes/js/geopoints.md +++ b/_includes/js/geopoints.md @@ -40,7 +40,40 @@ query.find({ At this point `placesObjects` will be an array of objects ordered by distance (nearest to farthest) from `userGeoPoint`. Note that if an additional `ascending()`/`descending()` order-by constraint is applied, it will take precedence over the distance ordering. -To limit the results using distance, check out `withinMiles`, `withinKilometers`, and `withinRadians`. +To limit the results using distance, check out `withinMiles`, `withinKilometers`, and `withinRadians`. Use the `sorted` parameter to sort the results by distance ascending. + +

+var location = new Parse.GeoPoint(37.708813, -122.526398);
+var distance = 5;
+var sorted = true;
+
+var query = new Parse.Query(PizzaPlaceObject);
+query.withinKilometers("location", location, distance, sorted);
+query.find({
+  success: function(pizzaPlacesInSF) {
+    // Pizza places within 5km sorted by distance
+    ...
+  }
+});
+
+ +If you add an additional sorting constraint set the `sorting` parameter to `false` for better query performance. + +

+var location = new Parse.GeoPoint(37.708813, -122.526398);
+var distance = 5;
+var sorted = false;
+
+var query = new Parse.Query(PizzaPlaceObject);
+query.withinKilometers("location", location, distance, sorted);
+query.descending("rating");
+query.find({
+  success: function(pizzaPlacesInSF) {
+    // Pizza places within 5km sorted by rating
+    ...
+  }
+});
+
It's also possible to query for the set of objects that are contained within a particular area. To find the objects in a rectangular bounding box, add the `withinGeoBox` restriction to your `Parse.Query`.