However, I was interested to know if there is a way to specify sorting by the create_date attribute, and not by distance ...
If you use the $near command, then you need to sort by distance first, or the concept of "near" doesn't really make any sense. On the globe, everything can be "close" to a given point, it is simply a question of "how close".
You have two options:
- limit results
$near - use the
$within command
I think you are looking for the $within command
center = [50, 50] radius = 10 db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})
Then you can sort them using another key:
db.places.find(...).sort({created:1})
However, there may be too many results within the command, so you probably want to set some logic to limit the number of elements returned by $within .
db.places.find(...).limit(50).sort({created:1})
True, if you hit a certain limit, the value of your $within command usually starts to fall. Your client code might want to check if you have achieved maximum results.
source share