Skip to content

Commit 0eda14c

Browse files
mtrezzaflovilmart
authored andcommitted
Add js geoquery sorted parameter (#529)
* added mongoDB performance caveat when using near and sorting constraint * added examples for sorted parameter
1 parent 8d465f1 commit 0eda14c

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

_includes/js/geopoints.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,40 @@ query.find({
4040

4141
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.
4242

43-
To limit the results using distance, check out `withinMiles`, `withinKilometers`, and `withinRadians`.
43+
To limit the results using distance, check out `withinMiles`, `withinKilometers`, and `withinRadians`. Use the `sorted` parameter to sort the results by distance ascending.
44+
45+
<pre><code class="javascript">
46+
var location = new Parse.GeoPoint(37.708813, -122.526398);
47+
var distance = 5;
48+
var sorted = true;
49+
50+
var query = new Parse.Query(PizzaPlaceObject);
51+
query.withinKilometers("location", location, distance, sorted);
52+
query.find({
53+
success: function(pizzaPlacesInSF) {
54+
// Pizza places within 5km sorted by distance
55+
...
56+
}
57+
});
58+
</code></pre>
59+
60+
If you add an additional sorting constraint set the `sorting` parameter to `false` for better query performance.
61+
62+
<pre><code class="javascript">
63+
var location = new Parse.GeoPoint(37.708813, -122.526398);
64+
var distance = 5;
65+
var sorted = false;
66+
67+
var query = new Parse.Query(PizzaPlaceObject);
68+
query.withinKilometers("location", location, distance, sorted);
69+
query.descending("rating");
70+
query.find({
71+
success: function(pizzaPlacesInSF) {
72+
// Pizza places within 5km sorted by rating
73+
...
74+
}
75+
});
76+
</code></pre>
4477

4578
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`.
4679

@@ -95,4 +128,5 @@ At the moment there are a couple of things to watch out for:
95128

96129
1. Each Parse.Object class may only have one key with a Parse.GeoPoint object.
97130
2. Using the `near` constraint will also limit results to within 100 miles.
98-
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.
131+
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.
132+
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.

0 commit comments

Comments
 (0)