Support for geospatial queries in Mongodb

Bug #727942 reported by Timo Westkämper
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Querydsl
Fix Released
Undecided
Unassigned
Changed in querydsl:
status: New → Fix Committed
Revision history for this message
Timo Westkämper (timo-westkamper) wrote :

The support for the near-function has been made available via custom types

The MorphiaAnnotationProcessor declares a custom-type mapping from Double[] to Point like this :

  configuration.addCustomType(Double[].class, Point.class);

Now you can use Double arrays in your domain class and they will be mapped to the Point class :

@Entity
public class GeoEntity {

    private @Id ObjectId id;

    private Double[] location;

   // ...

}

The Point class provides the methods needed for "near" usage :

public class Point extends ArrayPath<Double>{

    public BooleanExpression near(double latVal, double longVal){
        return BooleanOperation.create(MongodbOps.NEAR, this, new ConstantImpl<Double[]>(new Double[]{latVal, longVal}));
    }

}

Here is a test case which demonstrates the usage :

    @Test
    public void Near(){
        ds.save(new GeoEntity(10.0, 50.0));
        ds.save(new GeoEntity(20.0, 50.0));
        ds.save(new GeoEntity(30.0, 50.0));

        List<GeoEntity> entities = query().where(geoEntity.location.near(50.0, 50.0)).list();
        assertEquals(30.0, entities.get(0).getLocation()[0].doubleValue(), 0.1);
        assertEquals(20.0, entities.get(1).getLocation()[0].doubleValue(), 0.1);
        assertEquals(10.0, entities.get(2).getLocation()[0].doubleValue(), 0.1);
    }

Revision history for this message
Timo Westkämper (timo-westkamper) wrote :

Released in 2.2.0-beta1

Changed in querydsl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.