Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mongodb annotation for 2dsphere index for a geospatial field in java...?

@JsonSerialize
@Document(collection = "fence")
@CompoundIndexes({
        @CompoundIndex(name = "loc_groupId_idx", 
                       def = "{ 'loc': 2dsphere, 'groups.groupId': 1 }",      
                       unique = false) })
public class GeofenceMongoVO {

  public GeofenceMongoVO() {}

  @Id
  private String fenceId;

  @Field
  private Long customerId;

  @Field
  private String fenceName;

  @Field
  private Byte type;

This is how I tried to ensure a compound index on a geospatial field and a field of a child document(groupId). But this is not working unfortunately. Is there a way by which I can ensure 2dsphere index from java code via annotations?

like image 696
Sabya Avatar asked Sep 06 '25 03:09

Sabya


1 Answers

As of Spring Data MongoDB 1.10.10.RELEASE, you can annotate any field, whether it be at the document root or in a subdocument with:

@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private GeoJsonPoint myGeometry;
like image 116
user5365075 Avatar answered Sep 09 '25 04:09

user5365075