使用Mongodb截断和计算Spring中的位置
在我的应用程序中,我想显示一个热图,所以我需要这样的东西来构建它:使用Mongodb截断和计算Spring中的位置,mongodb,spring-boot,spring-data,aggregation-framework,Mongodb,Spring Boot,Spring Data,Aggregation Framework,在我的应用程序中,我想显示一个热图,所以我需要这样的东西来构建它: data: [{lat: 24.6408, lng:46.7728, count: 3},{lat: 50.75, lng:-1.55, count: 1}, ...] 我有: @Document(collection = "positions") public class Position { @Id private String id; private long timestamp; @Ge
data: [{lat: 24.6408, lng:46.7728, count: 3},{lat: 50.75, lng:-1.55, count: 1}, ...]
我有:
@Document(collection = "positions")
public class Position {
@Id
private String id;
private long timestamp;
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private GeoJsonPoint location;
private File file;
为了对真正接近的位置进行计数和分组,我考虑截断所有位置,然后将它们分组进行计数。例如:
{"type" : "Point", "coordinates": [-47.121311,-18.151512]}
{"type" : "Point", "coordinates": [-47.121312,-18.151523]}
{"type" : "Point", "coordinates": [-47.121322,-18.151533]}
结果:{“类型”:“点”,坐标:[-47.1213,-18.1515]},“计数”:3}
在外壳中,它类似于:
"$map": {
"input": '$location.coordinates',
"in": {
"$divide": [
{ "$trunc": { "$multiply": [ '$$this', 10000 ] } },
10000
]
}
}
然后我会根据位置、坐标进行分组。但我不知道这是否是正确的方法,也不知道如何在Spring中在我的PositionRepositoryCustom中编写此代码。我必须创建一个聚合,“进行映射”,然后对组进行计数
多谢各位
----编辑2---:
我创建了一个名为:
public class PositionSummary {
private List<Double> coordinates;
private Double count;
public PositionSummary() { }
public List<Double> getCoordinates() {
return coordinates;
}
public void setCoordinates(List<Double> coordinates) {
this.coordinates = coordinates;
}
public Double getCount() {
return count;
}
public void setCount(Double count) {
this.count = count;
}
}
再次感谢您。您的方法很好。请使用下面的聚合代码
AggregationOperation project1 =
Aggregation.project("type")
.and(
VariableOperators.mapItemsOf("location.coordinates").
as("coord").
andApply(
ArithmeticOperators.valueOf(
ArithmeticOperators.valueOf(
ArithmeticOperators.valueOf(
"$$coord"
).multiplyBy(1000)
).trunc()
).divideBy(1000)
)
.as("coordinates");
AggregationOperation group =
Aggregation.group("coordinates")
.first("type").as("type")
.count().as("count");
AggregationOperation project2 = Aggregation.project("count","type").andExclude("_id").andInclude(Fields.from(Fields.field("coordinates", "_id")));
Aggregation agg = Aggregation.newAggregation(project1,group,project2);
List<Document> results = mongoTemplate.aggregate(agg,"positions",Document.class).getMappedResults();
聚合操作项目1=
聚合。项目(“类型”)
.及(
VariableOperators.mapItemsOf(“location.coordinates”)。
作为(“合作”)。
安达普利(
算术运算符(
算术运算符.值(
算术运算符(
“$$coord”
).倍数(1000)
).trunc()
).除以(1000)
)
.作为(“坐标”);
聚合操作组=
聚合组(“坐标”)
.第一(“类型”)。作为(“类型”)
.count().as(“count”);
AggregationOperationProject2=Aggregation.project(“count”、“type”)。和exclude(“u id”)。和include(Fields.from(Fields.field(“coordinates”),“u id”);
Aggregation agg=Aggregation.newAggregation(项目1、组、项目2);
List results=mongoTemplate.aggregate(agg,“positions”,Document.class).getMappedResults();
谢谢您的回复,我也很感谢您能抽出时间。我用我所做的更新了我的答案,如果您能检查一下的话,谢谢您。不客气。您能试试AggregationResults groupResults=mongoTemplate.aggregate(agg,“positions”,PositionSummary.class)吗
variant?我如你所说做了更改,但现在我得到了空坐标。我再次更新了!欢迎你。这是预期的。更新的答案包括项目阶段。我添加了新的项目阶段,确实现在可以工作了,非常感谢Veeram!我发现这个主题非常困难,而且我没有找到很多示例和文档,所以非常感谢你的帮助。
[
{
"coordinates": null,
"count": 1
},
{
"coordinates": null,
"count": 1
},
{
"coordinates": null,
"count": 2
},
{
"coordinates": null,
"count": 1
},
{
"coordinates": null,
"count": 2
},
...]
AggregationOperation project1 =
Aggregation.project("type")
.and(
VariableOperators.mapItemsOf("location.coordinates").
as("coord").
andApply(
ArithmeticOperators.valueOf(
ArithmeticOperators.valueOf(
ArithmeticOperators.valueOf(
"$$coord"
).multiplyBy(1000)
).trunc()
).divideBy(1000)
)
.as("coordinates");
AggregationOperation group =
Aggregation.group("coordinates")
.first("type").as("type")
.count().as("count");
AggregationOperation project2 = Aggregation.project("count","type").andExclude("_id").andInclude(Fields.from(Fields.field("coordinates", "_id")));
Aggregation agg = Aggregation.newAggregation(project1,group,project2);
List<Document> results = mongoTemplate.aggregate(agg,"positions",Document.class).getMappedResults();