MongoDB version 2.4 will bring with it a host of exciting new features, and chief among them are the additional support for geo data. In addition to the 2d geospatial indexing and geospatial querying that mongo has supported for a while, the 2.3.2 development release introduces the “2dsphere” index and support for the GeoJSON format.
I was lucky enough to have the opportunity to help out during the QA process for this feature, and wanted to share a simple little demo I put together as a sanity check. What I really love is that I was able to put together this demo in a few short hours knowing only a little bit about the google maps JS api, and knowing nothing about mongo geo features. This is a huge boon to rapidly prototyping location aware systems!
You can toggle between the three new query types by selecting the name of the operation on the top bar.
$within: Click to add points points to the map. Once you have added three points, an initial polygon will be drawn, and the server will fetch up to 500 points or lines from the database, and they will be rendered on the map. You can click on points to see what landmark they represent. You can continue adding points to your polygon, but if it self-intersects at all, you will get no results. Self intersecting polygons are invalid.
$geoIntersects: Click to add points as with $within. Once you have added two points, a line will be drawn between them. The server will fetch some lines that intersect with that line. You can continue adding points, and you will generate a line string. The number of results is limited to 50.
$near: Click to add a point. Mongo will fetch the nearest 10 points/lines, and draw them on the map. It’s pretty fun to do this in the areas with water in them!
At any time you can clear any current interactions using the Clear button on the bottom left.
You can get the source code here
- This was a very quick project built to internally demo and test these features - there are many many things I would change if this were to serve production customers.
- Something funny happens with line colouring in this demo. I’m not sure if I am to blame, or Google.