mongodb$geonear在聚合管道中使用最新的java驱动程序

y0u0uwnf  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(331)

好吧,我要做的是找到在聚合管道中添加$geonear的正确方法。通过在聚合管道列表中添加org.bson.document,我已经成功地做到了这一点,但是我认为应该有一种更清晰的方法来使用最新的官方mongodbjava驱动程序实现它。例如,为了对$group、$project、$match执行此操作,mongodb java驱动程序提供了聚合类,相关调用如下: Aggregates.group , Aggregates.project , Aggregates.match . 我想要的是: Aggregates.geoNear ,表示缺席。我的示例代码如下所示:

  1. createDocumentCollection()
  2. .aggregate(
  3. Arrays.asList(
  4. // geoNear spherical geometry for 2d sphere
  5. new Document("$geoNear", new Document()
  6. .append("near", new Document()
  7. .append("$geometry", new Document()
  8. .append("type", "Point")
  9. .append("coordinates", Arrays.asList(longitude, latitude)))
  10. .append("$maxDistance", maxDistance))
  11. .append("key", "avgGeoPoint")
  12. .append("distanceField", "dist.calculated")),
  13. // group
  14. Aggregates.group("$mmsi",
  15. Accumulators.first("mmsi", "$mmsi"),
  16. Accumulators.first("vesselName", "$vesselName"),
  17. Accumulators.first("shipType", "$shipType")),
  18. // projection
  19. Aggregates.project(Projections.fields(
  20. Projections.excludeId(),
  21. Projections.include("mmsi", "vesselName", "shipType"))),
  22. // skip
  23. Aggregates.skip(options.getSkip()),
  24. // limit
  25. Aggregates.limit(options.getLimit())))
  26. .map(ModelExtractor::extractPlainVessel)
  27. .forEach((Consumer<PlainVessel>) nearVessels::add);

我要替换的部分是:

  1. new Document("$geoNear", new Document()
  2. .append("near", new Document()
  3. .append("$geometry", new Document()
  4. .append("type", "Point")
  5. .append("coordinates", Arrays.asList(longitude, latitude)))
  6. .append("$maxDistance", maxDistance))
  7. .append("key", "avgGeoPoint")
  8. .append("distanceField", "dist.calculated"))

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题