Using MongoDB geospatial index with Meteor.js
Learn how to find and index locations using MongoDB
Building a location query for your Meteor app will require a few steps to get going. I hit a few snags along the way when building my example app for my recent O’Reilly webcast, so I thought I would share some of the important points so maybe someone can avoid my mistakes :)

Build your index correctly
For me, working with GeoJSON was new, I wasn’t familiar with the various structures involved when I got started building my data. Here is what the JSON looks like for GeoJSON in my mongo setup:
{
"loc": {
"type" : "Point",
"coordinates" : [
-84.465176,
39.227033
]
}
}
The interesting parts to take away here are that we mark it as a point and that the longitude comes BEFORE the latitude, which seemed counter-intuitive to me. Pretty sure we were taught latitude then longitude in the olden days of my schooling.
Now that we have a GeoJSON point saved off in our MongoDB, we can easily add a Mongo 2d index using the the collection _ensureIndex method in Meteor.js:
Places._ensureIndex({'loc.coordinates':'2dsphere'});

Query your spatial off
So, we have a shiny new Geospatial index built in our Mongo database now, what do we do with it? Query it, of course! But HOW do we query it?
Simple, just build a $geoWithin query using a $box. In my Cordova example, I used leaflet and the getBounds() call and we will be able to get the bottomLeft and topRight for building our query:
Places.find( { loc : { $geoWithin :{ $box : [bottomLeft, topRight]} }})
One thing to note, Minimongo doesn’t support $geoWithin tag, so it is best to use this query in your server-side publication. In this particular bit of code, we are using the $box shape to run the query, giving it a bottomLeft corner and topRight corner to define the box. The $geoWithin tag also supports $center, $centerSphere, and $polygon.
You can see it all come together in my youtube version of the webcast:
// RELATED POSTS
Why you shouldn't wait for Meteor 1.0 (to start using it)
Meteor.js is ready to use right now, find out why!
Read the full breakdown →How to optimize your Mongo database for Meteor.js
Speed up your Meteor apps by optimizing MongoDB
Read the full breakdown →Meteor and Mongo Performance
Curious about how a top Meteor app is scaling? Learn here...
Read the full breakdown →