你的查询修改其实很简单,只需要在查询中加入 GeoJSON 类型的过滤条件即可:
db.BLR_all.find(
{
"geom" : {
"$geoWithin" : {
"$geometry" : {
"type" : "Polygon",
"coordinates" : [ [ /** array of points **/ ] ]
}
}
},
"geom.type": "Point"
})
或者特别是一些样本数据,
{
"_id" : ObjectId("534cb37acca5f311cc5c23cc"),
"loc" : {
"type" : "Polygon",
"coordinates" : [
[
[
0,
0
],
[
3,
6
],
[
6,
1
],
[
0,
0
]
]
]
}
}
{
"_id" : ObjectId("535aeddfb6b8fd04b3424a09"),
"loc" : {
"type" : "Point",
"coordinates" : [
3,
6
]
}
}
{
"_id" : ObjectId("535aef22b6b8fd04b3424a0a"),
"loc" : {
"type" : "Point",
"coordinates" : [
9,
12
]
}
}
发出查询:
db.geo.find({
"loc": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ]
}
}
},
"loc.type": "Point"
})
只会返回“点”且在坐标范围内的项目。
{
"_id" : ObjectId("535aeddfb6b8fd04b3424a09"),
"loc" : {
"type" : "Point",
"coordinates" : [
3,
6
]
}
}