【发布时间】:2017-10-20 16:59:13
【问题描述】:
您能帮我解决以下问题吗?
我在下面给出的存储库中有上述错误
@Repository("polygonQueryRepository")
@RequiredArgsConstructor
public class PolygonQueryRepositoryImpl extends PolygonRepository {
@Autowired
private MongoOperations operations;
public List<DBObject> findPolygonsMatchingGivenPointAndInputAggregate(Double lat, Double lng, String band) {
GeoJsonPoint point = new GeoJsonPoint(lat, lng);
MatchOperation operation = match(Criteria.where("location").intersects(point).and("attributes.band").regex(band));
Aggregation aggregation = newAggregation(Polygon.class,
unwind("attributes.transponders"),
operation);
AggregationResults<DBObject> result =
operations.aggregate(aggregation,"Polygon",DBObject.class);
return result.getMappedResults();
}
}
我之前读过在定义 MongoTemplate bean 时需要注册编解码器。所以我做了 - 仍然没有运气。
@Configuration
public class SpringMongoConfig {
@Value("${mongo.db.name}")
private String dbName;
@Value("${mongo.db.host}")
private String dbHost;
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
MongoClientOptions options = MongoClientOptions.builder().codecRegistry(MongoClient.getDefaultCodecRegistry()).build();
return new SimpleMongoDbFactory(new MongoClient(dbHost,options), dbName);
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
}
你有什么建议吗?
【问题讨论】:
-
非常不言自明。 MongoDB 不使用
GeoJsonPoint数据结构。由于这是一个自定义函数或至少在外部库中实现,因此您要么需要使用序列化程序进行扩展,要么(在这里有些意义)只需提供您应该执行的预期 BSON 结构。所以建议使用后者,特别是因为“所说的函数”实际上的顺序不正确,GeoJSON 的实际顺序是longitude,然后是latitude。如果没有正确的顺序,MongoDB 查询将无法工作。 -
事实上 Mongo 以这种方式准确识别 - 经度然后纬度。 GeoJsonPoint 来自 org.springframework.data.mongodb.core.geo - 所以它与 Mongo 和 Spring 的集成非常相关。
标签: java spring mongodb aggregation