【问题标题】:MongoDB query Points within Polygons多边形内的MongoDB查询点
【发布时间】:2020-08-09 23:30:13
【问题描述】:

我是使用 java 驱动程序的 MongoDB 新手.. 我尝试从一个集合中查询多边形并匹配另一个集合中的点。

我真正拥有的是:

//This is collection 1.. here i get my polygon definition.. the name of the element is geometry
Document polyDocument = mongoClient.getDatabase("restheart").getCollection("polygons").find(eq("properties.ISO_A2", "AT")).first();

//This is collection2.. here are the points saved which i want to select if they are within polygon.. the name of the element is location
MongoCursor<Document> Cursor = mongoClient.getDatabase("restheart").getCollection("coll2").aggregate(
                Arrays.asList(
                    Aggregates.match(geoWithin("location", polyDocument.get("geometry")),  <--- here is my problem, whats the correct syntax?
                    Aggregates.group("$hashtag", Accumulators.sum("Count", 1)),
                )
            ).iterator();

我现在用谷歌搜索了很多,但找不到一个很好的例子,如何使用 javadriver 进行查询

集合 1 的样本:

{
    "_id" : ObjectId("5e95d56e49ebb0e6b45a34c4"),
    "type" : "Feature",
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [ 
            [ 
                [ ... , ... ]
            ]
        ]
    },
    "properties": { "ISO_A2": "AT" }
}

集合 2 的样本:

{
    "_id" : ObjectId("5e90bf7b49ebb0e6b459a00f"),
    "hashtag" : "stayhome",
    "timestamp" : ISODate("2020-04-10T18:48:25.876Z"),
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            14.421425, 
            40.26084
        ]
    },
}

【问题讨论】:

  • 嗨。请提供样本数据
  • 哦,是的,当然,我为集合 1 和集合 2 添加了一个样本 :)
  • 你能在 mongo shell 中构造产生所需结果的查询吗?如果没有,请先弄清楚。
  • 我认为这是不可能的,因为我需要 2 个查询.. 第二个使用第一个查询的结果.. 我所需要的只是 geowithin 的正确语法.. 我在示例编码..所有其他事情都正常

标签: java mongodb mongo-java-driver restheart


【解决方案1】:

好吧,经过一些尝试和错误,我找到了解决方案..我不知道它是否最聪明,但它可以工作:)

//This is collection 1.. here i get my polygon definition.. the name of the element is geometry
Document polyDocument = mongoClient.getDatabase("restheart").getCollection("polygons").find(eq("properties.ISO_A2", "AT")).first();

Document geoDocument = polyDocument.get("geometry", Document.class);
String geoType = geoDocument.getString("type");
List geoCoord = geoDocument.get("coordinates", List.class);
BasicDBObject geometry = new BasicDBObject("type",geoType).append( "coordinates",geoCoord);

//This is collection2.. here are the points saved which i want to select if they are within polygon.. the name of the element is location
MongoCursor<Document> Cursor = mongoClient.getDatabase("restheart").getCollection("coll2").aggregate(
                Arrays.asList(
                    Aggregates.match(geoWithin("location", geometry) ),
                    Aggregates.group("$hashtag", Accumulators.sum("Count", 1)),
                )
            ).iterator();

我不知道回答他自己的问题是否正确,但也许它可以帮助其他人..

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 2012-08-23
    • 2011-12-10
    • 2022-10-07
    • 2015-02-19
    • 2022-07-28
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多