【发布时间】: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