Querying geospatial data in mongodb looks straight forward enough through the shell script, however, I’m trying to replicate some code in morphia (playmorphia).
Get all points within a certain radius, docs say:
> center = [50, 50]
> radius = 10
> db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})
Here’s my code in my Peak model:
Double[] loc = new Double(2);
// it's set to my [Longitude, Latitude]
List<Peak> peaks = play.modules.morphia.Model.ds().find(Peak.class).field("loc").near(loc[0], loc[1], 10/111.12).limit(50).asList();
It works but am I doing something wrong by using the ds().find()? Is there a more elegant way to do this using the model e.g. Peak.find..? Thanks!
You need to put indexes on geo fields.
Sample code from my project:
Check the manual:
http://code.google.com/p/morphia/wiki/Query#Geo-spatial