【问题标题】:Spring mongodb annotation for 2dsphere index for a geospatial field in java...?java中地理空间字段的2dsphere索引的Spring mongodb注释......?
【发布时间】:2017-12-01 15:53:17
【问题描述】:
@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 索引?

【问题讨论】:

  • 你有一个错字,这就是它不起作用的原因。应该是"{ 'loc': '2dsphere', 'groups.groupId': 1 }",并在“2dsphere”周围加上引号''

标签: spring mongodb spring-mongo spring-mongodb


【解决方案1】:

我不确定是否可以使用注释来完成,但我发现了一篇博客文章 here,他们在其中使用了 ensureIndex。类似的东西

@Autowired
MongoTemplate template;

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 不能更准确,应该更新为好的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2016-10-01
    • 2011-10-23
    • 2016-02-12
    • 2019-11-30
    • 1970-01-01
    相关资源
    最近更新 更多