对于java中的地理空间字段的2dsphere索引的Spring mongodb注释...?

时间:2017-06-28 10:23:11

标签: spring mongodb spring-mongo spring-mongodb

@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;

这就是我尝试确保地理空间字段和子文档字段(groupId)上的复合索引的方法。但不幸的是,这不起作用。有没有办法通过注释从java代码中确保2dsphere索引?

2 个答案:

答案 0 :(得分:1)

我不确定是否可以使用注释完成,但我找到了一篇博客文章here,他们使用ensureIndex执行此操作。像是

之类的东西
@Autowired
MongoTemplate template;

public void setupIndex()
{
    template.indexOps(Location.class).ensureIndex( new GeospatialIndex("position") );
}

答案 1 :(得分:0)

从Spring Data MongoDB 1.10.10.RELEASE开始,您可以注释任何字段,无论是在文档根目录还是在子文档中:

@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private GeoJsonPoint myGeometry;