Question

I'm using Play Framework v1.2.5 and the MongoDB Morphia module PlayMorphia v1.2.12 connecting to MongoDB v2.4.5.

I can't figure out how to do a geospatial query using Morphia.

For example, a basic query is fine:

//save a test address
new Address(47.5, -122.3, "123 South Main Street", "Seattle", "WA", "98109").save();

//now find it
List<Address> address = Address.q().filter("city", "Seattle").filter("street", "123 South Main Street").asList();
assertEquals(address.get(0).street,"123 South Main Street");

However, the documentation makes no mention about how to use MongoDB's $geoWithin $geoIntersects $near $nearSphere queries.

I tried using something like this, as hinted at in the documentation, but it didn't work.

List<Address> e = Address.q().filter("latlon near","[47.5, -122.3]").asList();
Was it helpful?

Solution 2

I found a way to accomplish this, however, it's pretty ugly because the result needs to be casted.

List<Address> addresses = (List<Address>)(List<?>) Address.q().field("latlon").near(47.5, -122.3).asList();

OTHER TIPS

I think the ultimate problem is that your parameter to filter is a String and not a double[]. Try something like: Address.q().field("latlon").near(47.5, -122.3). if you want $geoWithin specifially, you can use .field("latlon").within(Shape.center(new Point(47.5, -122.3), someRadius). You'll need morphia 0.104 for that version of within().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top