I finally started looking at the basics regarding Mongo DB and I'm fascinated by the simple way to work with it. Mongo DB is a NoSQL database and NoSQL databases are very different compare to relational databases like mysql where you need to really have a map for everything and you need to figure out the exact schema including what tables you use and what fields you'll use and even the types of fields. NoSQL databases don't have to do anything like that, (plan the structure of your database and collections...) you don't need to do any predefined structuring before you build your application. NoSQL databases are equipped to handle rapidly expanding data and the growing complexity of data structures. NoSQL databases have became very popular for giving the user a more flexible and scalable experience.
Today I'm going to be introducing Mongo DB to you!
After you download Mongo DB go ahead to your terminal and open mongo DB by typing 'mongo'.
Once you are in, type in your terminal:
show dbs
and that will show you all the databases that we have.
You will see that Mongo DB has three default databases that you start with, Admin, Config and Local. In the picture you will see more databases from other projects I have thou.
Let's go ahead and start fresh. Let's create a new database which we will call 'new' for lack for imagination, and to do that you will need to type:
use new
That will create a new database 'new' and mongo DB will switch you to that database automatically, it will give you message of 'switched to db new'. If you go ahead and now check on your databases you should probably see your database there, right? Nope. The reason because it doesn't show it yet is because we haven't created a collection yet inside your 'new' database.
If that's the case then let's go ahead and create a collection!
first lets go ahead an check what database we are in just in case and to do that you need to type in your terminal:
db
It will show us that we are still in our new database and then we will create our collection which we will call 'first':
db.createCollection('first')
This will create our collection and if you want to see if it is there you can type in your terminal:
show collections
and your collection will show!
now let’s write a document we can work with and to make it more readable I'm going to write it first in a separate file.
The way you add something on your collection is by using insert.
db.first.insert()
and you pass an object which can be anything you want. I am going to do a simple one that will include my first name, last name, where I am from, an array of my favorite things to do and I'm going to add a date as well.
Now that I have it on my file I am going to copy and paste it to my terminal to add it to my collection. If it was successful it will give you WriteResult({ "nInserted" : 1 })
Ok, I added a second object and now that it has been inserted to our collection, you can find the data by typing this command in your terminal
db.first.find()
Which will show you the documents we have in our collection. The problem is that it is going to look weird and it is going to be hard to read. There is a solution for this problem, there is a method call pretty that you can use to make it easier to read. You will type in your terminal:
** db.first.find().pretty()**
I did it both ways so you can see the difference.
Other features are:
You can now select the specific data with find. You just need to be more specific, for example:
db.first.find({country: 'United States'}).pretty()
This will display the documents with that value.
We can use a sort method too where 1 is ascending and - 1 is descending, for example:
db.first.find().sort({ _id: -1 }).pretty()
That will give you the documents starting with the last one added.
You can even put a limit at the number of documents with limit.
db.first.find().limit(1).pretty()
That will show you only one object form the collection.
The last thing I'm going to show you is that if you want to delete your database all you have to do is type:
db.dropDatabase()
and your database is gone!
Conclusion:
In my view Mongo DB is something I'll be using more often because of the flexibility it gives the user. Mongo DB is faster and handles highly diverse data types. I like that
Mongo DB has better ability to handle large unstructured data and there are no restrictions on schema design.
Have a wonderful day and I hope you enjoyed this blog!