I have a collection of geoJSON data in a mongoDB database (v2.6.10 ). The data consists of thousands of polygons, mostly squares that represent 500mx500m land claims. This data is being used on a web app that displays the data on a map.
The web app performance is really slow due to the huge number of polygons. To improve performance I want to limit the queries to the bounds of the current map window. We are using the Mapbox API and I am able to get map bounds from the API to be used in a mongodb geo-query.
So far I have been unable to get the mongo queries to cooperate.
Here is a sample record of the geoJSON data that is stored in the DB:
{
"_id" : ObjectId("56ca94e0336aac299182442f"),
"type" : "Feature",
"properties" : {
"TNRTPCD" : "P",
## some fields ommitted for privacy reasons ##
"TRMNTNDT" : null,
"RNHCTRS" : 20.3496,
"TTLTPCD" : "PCX"
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [
[
[
[
-121.88883540217063,
50.97489195799901,
0
],
[
-121.88883523174192,
50.97072525131302,
0
],
[
-121.8950854466247,
50.97072527980969,
0
],
[
-121.89508560169767,
50.97489198254216,
0
],
[
-121.88883540217063,
50.97489195799901,
0
]
]
]
]
}
}
I have tried the following query and it returns no records and no errors:
db.docs.find(
{
'geometry.coordinates': {
$geoWithin: {
$geometry: {
type : "Polygon" ,
coordinates: [[
[ -121.26571655273438, 49.56352444783811 ],
[ -121.26571655273438, 52.719152198289635 ],
[ -120.97492218017578, 52.719152198289635 ],
[ -120.9749221801757, 49.56352444783811 ],
[ -121.26571655273438, 49.56352444783811 ]
]]
}
}
}
}
)
I know that there is data within the defined boundary and yet noting returns. I have tried playing around with the target column. I tried switching 'geometry.coordinates' to just coordinates, I tried with quotes and without. I also messed around with the polygon AOI coordinates. When the first point is not repeated I get an error.
I have also tried switching $geoWithin for $near, $box and $geoIntersects. With similar results.
I'm guessing that the issue has to do with the geographical index but that's just a hunch. I tried to put a 2dsphere index on the geometry column using the following command:
db.docs.ensureIndex( { "geometry" : "2dsphere" } );
That fails and gives the following error:
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"ok" : 0,
"errmsg" : "Can't extract geo keys from object, malformed geometry?:
When I try putting an index on just the coordinates it seems to work. I used this command:
db.docs.ensureIndex( { "coordinates" : "2dsphere" } );
And got the following output:
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
To be honest I am new to mongodb so this feedback doesn't mean much to me. I am not new to GIS and mapping but I can't figure this out. Please help. To summarize, I'm trying to narrow my query results by using mongo's geo filtering functions.
x/y/zvalues, and you can only havex/yvalues. So drop of the0values forzthat you would not be using anyway. - Blakes Seven$geoWithinwithout an index, though an index does help speed the search. - Blakes Seven