[MUSIC] >> Hello and welcome back. In this lecture, we will take a look at geospatial introduction, 2dsphere, 2dsphere indexes and queries, and we look at some examples with built in support using near, minDistance and maxDistance. So what is geolocation? Well, as you know, geo refers to Earth and location is position on the Earth, so it's basically position on the Earth, right? Geolocation. The geolocation services is a massive multi-billion dollar industry today. If you have a smartphone, you have a bunch of apps, like Groupon, Starbucks, Bacon app, it has some kind of geolocation services built into it. And these social media apps will track you down and will push you local deals and locals news and local specials, like Starbucks and Groupon will basically give you this notification saying hey, you just entered a mall and there is a Starbucks here. And it's all based on position and the geolocation services. So just to give you a broad example, and we kind of look at that in our example and say hey, give me all the closest cities, five cities maybe, that is closest to this zip code, right? Same example can be extended and said hey, give me a list of Starbucks close by to this zip code. So you can make multiple examples like that and there are a lots of Enterprise applications out there today where you can type in a zip code and they'll give you the health care providers that are part of your insurance today. And that's how you can go and pick your doctor or some specialist and the technology that is driving behind is a geolocation services. Now MongoDB allows you to create, index and query geospatial data. And we know that geospatial data is basically, again, location data. Now the data is stored in spherical surface or what we call 2dsphere index in Mongo. Now what is 2dsphere index? Now it's basically an index that support geometric calculations on earth-like sphere. Okay? Now the default data on this earth-like sphere, the coordinate-axis that is longitude, latitude. So it's a square parenthesis, a number, number which represents longitude, latitude. Now that is the default. 2dsphere index also supports geospatial queries, like inclusion, intersection, proximity, so you can give it a zip code and say, give me the intersection, proximity to the zip code, and we'll run a couple of these examples to show min, max distance, and all that, to get some rich information back from the database. The 2dsphere index supports data stored as GeoJSON. What is GeoJSON? GeoJSON is basically an open standard format. It was designed for representing simple geographic features like, for example, Point, which could be a location on the Earth. In this example, I have Point 40, 5, which is longitude, latitude or they could also be things like, we have LineString, which could represent a street or a highway on a map. Polygons could be a big section of a country or a state. And similarly you have other things like MultiPolygon, Geometry Collection. So you can go and look up more information on GeoJSON objects, but it's basically, again, a rich open standard for Mac. It's GeoJSON. It's Geo as in Earth and JSON as in JavaScript Object Notation. Right, so some common examples like your navigation apps in your phone or your GPS is a good candidate where they might describe their service coverage using GeoJSON. Now here's an example of creating an index, so it's create_one and then you will take a field in your document, location in this example, and you will go and create an index on that. Now, one important thing to remember here is that if you look at our zips data that we have, the location actually contains the data in latitude, longitude. Right? It's exactly the opposite to GeoJSON. Now, so while longitude, latitude is the default, nothing is stopping you from using this format, but the important thing is the data format and the query syntax must be consistent. So when you get the data back, you want to make sure that you're expecting the data in latitude, longitude or longitude, latitude. So whatever is your data, make sure your query is consistent with that. Okay, let's start off by creating an index. So here is our create_one. So that successfully goes and creates an index for us. Okay? So, let's start off by first, fetching the city called Baltimore and let's try to see what the latitude and longitude is. Okay, so that's the command or query db[:zips].find(:city => 'BALTIMORE').first. The list comes back with the location, longitude, latitude. So it's saying that -76 is the longitude and 39 is the latitude. That's the population and all that. So based on this data, we can go and use a geospatial query to find five closest cities, with some other parameters like minDistance and maxDistance and all that. So let's go and use this data as a source of our next query. So as I was saying, let's go find some city close to Baltimore using near, minDistance, and maxDistance. So here's the command or query, find location near coordinates 76 longitude, latitude. Now here's minDistance and maxDistance. 10,000 and 50,000 is meters. Now, 2dsphere index only accepts or takes in meters, so if you have your client application taking in input in miles or kilometers or any other data format, which is completely okay, you just need to do the appropriate conversion or translation before using this 2dsphere index, because it only takes in meters. So let's go and try this command and this should give us some data based on that coordinates that we have. So based on this location, let's just paste the command here. So find coordinates close to that location. We should get a bunch of cities. So here, it's limited to five. So we've got five cities, like Linthicum Height, Catonsville in Maryland, with some location and population and all that. So, simple query to get data based on geolocation or geospatial data. So just to quickly summarize, geospatial indexes and query will help in fetching data based on proximity. And it's very useful when building custom pages that are geo-specific, and in fact, the next thing we'll go into is a demo where we will take advantage of this geospatial and build, and basically show a map using geospatial to get cities closer to like the example we just did. So this wraps up geospatial and we will jump into the demo and I'll see you in the demo lecture, thank you.