好吧,我要做的是找到在聚合管道中添加$geonear的正确方法。通过在聚合管道列表中添加org.bson.document,我已经成功地做到了这一点,但是我认为应该有一种更清晰的方法来使用最新的官方mongodbjava驱动程序实现它。例如,为了对$group、$project、$match执行此操作,mongodb java驱动程序提供了聚合类,相关调用如下: Aggregates.group
, Aggregates.project
, Aggregates.match
. 我想要的是: Aggregates.geoNear
,表示缺席。我的示例代码如下所示:
createDocumentCollection()
.aggregate(
Arrays.asList(
// geoNear spherical geometry for 2d sphere
new Document("$geoNear", new Document()
.append("near", new Document()
.append("$geometry", new Document()
.append("type", "Point")
.append("coordinates", Arrays.asList(longitude, latitude)))
.append("$maxDistance", maxDistance))
.append("key", "avgGeoPoint")
.append("distanceField", "dist.calculated")),
// group
Aggregates.group("$mmsi",
Accumulators.first("mmsi", "$mmsi"),
Accumulators.first("vesselName", "$vesselName"),
Accumulators.first("shipType", "$shipType")),
// projection
Aggregates.project(Projections.fields(
Projections.excludeId(),
Projections.include("mmsi", "vesselName", "shipType"))),
// skip
Aggregates.skip(options.getSkip()),
// limit
Aggregates.limit(options.getLimit())))
.map(ModelExtractor::extractPlainVessel)
.forEach((Consumer<PlainVessel>) nearVessels::add);
我要替换的部分是:
new Document("$geoNear", new Document()
.append("near", new Document()
.append("$geometry", new Document()
.append("type", "Point")
.append("coordinates", Arrays.asList(longitude, latitude)))
.append("$maxDistance", maxDistance))
.append("key", "avgGeoPoint")
.append("distanceField", "dist.calculated"))
暂无答案!
目前还没有任何答案,快来回答吧!