简体   繁体   English

从mongodb中的$ geoNear聚合获取距离字段

[英]Get distance field from $geoNear aggregation in mongodb

I have the following aggregation command in my Spring project: 我的Spring项目中有以下聚合命令:

NearQuery query = NearQuery.near(longitude,latitude).maxDistance(distance).spherical(true);
agg = newAggregation(
                geoNear(query, "distance"),
                unwind("rate"),
                group("id")
                        .first("name").as("name")
                        .sum("$rate.general_rate").as("rate")
                        .count().as("num_rates")                                                                                        
        );

But when i made the mapped with my class, the distance field(from geoNear) is not exist. 但是,当我使用我的课程进行映射时,(来自geoNear的)距离字段不存在。 How can i pass in pipeline the distance to appear with others groups fields? 如何传递管道与其他群组字段一起显示的距离?

I faced the same :) 我面对同样的:)

The answer is in your question.. 答案就在您的问题中。

geoNear(query, "distance"), geoNear(query,“ distance”),

The above line will look for the property distance (type Double in case of Point) in the class which you are trying to aggregate the result. 上一行将在您试图汇总结果的类中查找属性距离 (在Point的情况下,键入Double)。

Eg., output "distance" : 420.82602810248557 (in meters ) 例如,输出"distance" : 420.82602810248557 (以米为单位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM