I decided to create a full Web Developer Course that will cover all the technologies that you need to kickstart your Full Stack Developer Journey. No bullshit, everything practical and relevant will be covered.
We will delve into following topics –
Check out the first video of this series where we cover the introduction to World Wide Web and the Evolution of the World Wide Web. The code for the video (if any) can be found on GitHub.
In this article, let us learn about Running Geospatial Queries in MongoDB .
Geospatial Queries – Finding Places
Geospatial Queries are an interesting thing in MongoDB. You can fire queries not just for text, boolean, number, dates as condition but you can also create queries for locations like :
Find me all the restaurants within a radius of 2km.
Find me all the hospitals that near to this specific place
Running Geo Queries
Let us try to find all the places that are near my current location. So for this, let’s run this query. Make sure to use the latitude and longitude of your location before you fire the below query :
And here the $geoNear is the behind the scenes name of our $near query. For this we will need the geospatial index for this query to run. Not all the geospatial queries require the index but they all will somehow benefit from having such an index.
To add such an index we can use the createIndex method on the collection.
And if we repeat the same query, it should now succeed.
Now the question that must come in our mind is that how is near defined near, meaning that relative to what it is near. It does not make sense unless we restrict it.
We can also define $maxDistance which is a value present in metres here. We can also define the $minDistance which is also a value define in metres
Let us now write a query to find all places that are near to us in a certain radius distance.
Find out all places that are near to us in a certain radius distance
This answers our first question regarding which points are near to our current location. Now this area could either be in form of a sphere, polygon etc., let us say we want to find out which points are inside of that area ?
This is another typical question that we often encounter and in order to answer this let us add more points to our database. Let us add three more places :
Now let us run a query to find all the places that lie inside a certain area :
For finding such places, go to Google Maps : Inside the Google Maps Section
Go to Your Places tab.
Create a new map there.
Let us draw a polygon around our location.
$geoWithin will help us to find all the elements within a certain shape or certain object typically like a polygon. $geoWithin takes a document as a value and here we can add a geometry object which is just the GeoJSON Object
Store all the four coordinates inside the points p1, p2, p3, p4
Finding out if a user is inside a specific area. This can also be done using geospatial queries.
Let us see how we can find places within a certain area :
This is what we get as a result :
The $near method gives us the list of the places in the sorted order whereas the $geoWithin method will give us the list of the places in the unsorted order but we can sort them using the sort method on the records that we get back.
So this is it for this article. Thanks for reading.
If you enjoy my articles, consider following me on Twitter for more interesting stuff :
⚡Twitter : https://twitter.com/The_Nerdy_Dev
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.
Hey guys 👋🏻, In this article, let us understand about The NoSQL Database : Basics of CRUD in MongoDB. So in this article we will learn about how to retrieve to retrieve/access, delete, create and update our documents. We will also see how we can delete a document by making use of its id and all that jazz. We will be covering the basics about collections and documents, the basic data types and we will also see how we can perform these CRUD operations. So without a further ado, let’s get started.
Database, Collections and Documents
In the MongoDB world, you can have one or more databases. Each database can hold one or more collections. Now what is a collection ?
A collection is just like a table that we have in a relational database like SQL. In a collection that you and I create, we can have multiple documents. Something that we will discuss a bit later.
Now what do we mean by documents ?
A document is essentially a data piece that you store in the database.
You don’t need to worry about these much. When working with MongoDB database, collections and documents gets implicitly created for you when you start storing the data and work with them.
A bit later in this article we will also learn an explicit way of creating documents in your collection using which you can also configure them a bit further.
The Shell and the MongoDB drivers for different languages
You will use MongoDB drivers in the real world applications depending on the programming language you are using for writing the backend servers on.
Shell is the only approach that will work for every language.
MongoDB CRUD Operations
Shell is what gets connected to the local mongo server.
Creating Databases and Collections
To check what databases, we have on the server we can run the command :
Now to switch to a database, we can make use of the use command. If the database exists, then the use command in that case will make use of the existing one otherwise it will create the new one on the fly so basically on the execution of the command. So let us create a database and call it as flights
So to switch to the flights database, we can say :
We store collections in database. But in order to access a document we need to access that respective collection first in which the document resides, then only we will be able to access the document.
Understanding JSON data
Every document you enter gets a new unique id which is the feature of the MongoDB. You need to have a unique id for each and every document. We do not have to specify it on its own. Mongodb does this for us under the hood which is of type ObjectId which is another type supported by MongoDB. This id will allow you to sort the documents because it has some timestamp associated to it. This is how JSON data gets stored into our database, to be precise into the collection of our database.
To list all the databases that are there on the MongoDB server, we can run the command :
and this would give us something like this :
Now let us switch to the flights database. Now currently as you can see, the flights database does not exists. So to create it on the fly, we can make use of the command use flights and this will automatically generate a flights database for us.
As a result of the above command, we get this as output in our terminal :
switched to db flights
Let us insert a new flight into the flightData collection which gets implicitly created on demand. Now this new flight is a new document which gets inserted in the flightData collection of the flights database which we reference as db. So db here represents the current database to which we are connected to.
Now on running the above query, we get this as the output :
To display the array of flight documents in a pretty formatted manner.
First we reference the current active database db.
Then reference the collection whose all the documents we want to retrieve which in our case is flightData
The find() method retrieves all the documents in the current referenced collection.
So this is our query for same :
and this is the output for same :
Let us now do a comparison between JSON and BSON.
Comparing JSON and BSON
This is what JSON looks like :
JSON is what we insert or retrieve. Behind the scenes, MongoDB actually uses BSON data. This conversion is done by the drivers. This is simply done because BSON is more effective to store than the JSON data, faster and more efficient in terms of size and efficient storage. There are different types of data – numbers, binaries etc. which are stored in different ways behind the scenes
BSON stands for Binary JSON for storing data into the database.
Let us insert one more flight in our flightData collection.
This above insertOne does work because the collection can accept mixed documents since we know MongoDB is schemaless.
Two documents in the same collection don’t necessarily need to have the same schema
We may have the schema but it is not the must. The id which is autogenerated you just don’t have to use the autogenerated id you just have to ensure that you have a unique id but if you cannot ensure this you can assign the ids on your own.
Let us insert the same document which we inserted which had missing schema with the id of our choice. Make sure the id that you use is unique.
So here is the query for same. Please note here we are using an id of our choice i.e lmn-kpq:
After inserting the document into the flightData collection, we can find all the documents within the collection
Here the id is unique and it is the one that we used while creating the new document. It doesn’t have ObjectId type for the simple reason because it was not autogenerated by MongoDB it is the id that we gave to the document. In the next article, we will learn more about CRUD operations. See you next time !
Thanks for reading.
If you enjoy my articles, consider following me on Twitter for more interesting stuff :