All right now that you've got MongoDB installed on your local system the next step is to go ahead
and use it
right?
So let's head over to the www.mongodb.com website and we're going to navigate to the documentation
through the docs tab and then we're going to head over to getting started.
And if you take a look over here, there's the MongoDB CRUD operations.
I recommend that whenever you're exploring a new database system the first thing to wrap your head around
is how do you perform CRUD using that particular database.
So we saw how we can create, read, update and delete data using a SQL based system such as SQL lite
in the last module. In this section
we're going to look at how we can do that using MongoDB on the command line.
So this is the time to open up your hyperterminal. And after the prompt I'm going to type mongod or
some people call it 'mongod', and this will spin up our mongo server
when you hit enter. Once you see waiting for connections on port 27017 then you are ready to go.
This is kind of similar to how we had a localhost 3000.
But in this case we've got a local database that's being served up on this particular port.
So now if you're go into Shell and new tab or if take a look here at the shortcut which on Mac is COMAND +
T, then we can open up a brand new terminal. And this is a different connection because if you notice
him where on MongoDB database is running
we actually don't see our prompt.
We can't type something in here and we can't for example cd over to the desktop over here because we
don't have an active connection to our terminal.
This terminal is connected to the MongoDB database.
In our new terminal however we do now have a prompt and we can now tap into the Mongo shell. And to do
that
the key word is mongo without the 'd'.
And now if we hit enter and you see your prompt being changed to this > then you are ready
to use the Mongo shell.
So the Mongo shell is basically just a way for us to be able to interact with our MongoDB databases on our
local system using the command line.
But before we create a brand new database, it's good practice to take a look at what databases we already
have. How do we do that?
Well if at any point you get stuck and you're inside the Mongo shell
so you've got your little angle bracket showing there, you can always type help, as a cry for help, and
you will be able to get a helpful list of things that you can do while using the Mongo shell.
So the one that I want to highlight to you is this one which is the command show dbs. And this shows
the names of the database that we currently have on our local system.
So let's go ahead and try that out. Show dbs. Hit enter. And you can see that by default when you install
MongoDB, it comes preloaded with three databases: admin, config and local and you can see that all
of them are taking up zero gigabytes of space because they're completely empty.
This is what it would look like if you have a brand new configuration of MongoDB.
So let's go ahead and create our very first database. And to do that all we have to do is type use
and then we specify the name of our database.
So let's say the I wanted to create a database again for my shop so I might call my database shopDB.
I can write use shopDB and hit enter.
So now Mongo tells me that I've switched to the database that is called ShopDB.
But if I go back and I write show dbs, I don't see shopDB anywhere.
Well that's because for it to be listed, it has to have some content.
So we first have to give it some data.
We're now ready to tackle the 'C' in CRUD and see how we can create data using MongoDB.
If you ever want to know which database you're currently in, you can always type db hit enter and it
tells you that you're currently working within the shop database.
So how do we create data in our database?
This is the time to head back over to our documentation where we've got the MongoDB CRUD operations
explained to us.
So what we want to do is to create and you can see that in order to create one entry or many entries
into our database, the syntax looks something like this.
We write db.collection and this collection is actually the name of a collection.
And if that collection name doesn't currently exist then by writing this line and by executing it, it
will actually create that collection. And then we use a method such as insertOne or insertMany.
And this is an example of how you would use this method.
So for example if you wanted a collection called users inside your database you would say db.users.
insertOne.
And inside the method insertone you would pass over a Javascript object which will be the document.
So it will have fields and it will have values.
And this pretty much follows the key value pairs that we've been seeing in Javascript objects.
Let's take what we learn here and let's implement it for our shopDB.
So let's say db. and the collection that we want to make is the one that's called products. Collections
in MongoDB is kind of similar to tables in the SQL world.
They are a collection of documents and a document is simply just a single data record,
so that would be a single row
if you were using a SQL based database. If this collection products doesn't currently exist in our
database then by executing this line of code it will create that collection called products. And then
we write another . and we call the method insertOne and we open up a set of round brackets or parentheses.
As the inputs for this method
insertOne we get to insert a Javascript object. The product that we want to insert is the pen product
that we had previously.
So in this case it will have an id of 1,
it will have a name of let's say Pen and it will have a price of 1.20.
This is all the data that I want to add to each record or each document inside my product's collection.
And when I hit enter my data gets added to my database. And now I can use the key words show collections
and it will show me all the collections that currently exist in the current database which remember
is shopDB.
So I've only got one collection which is called products. And in my products I've only got one entry
which is my pen product.
This is how easy it is to add data to our database using MongoDB.
And if you look at the documentation you can see that you can either insertOne or you can insertMany.
And if you click on it you can see the syntax as well as examples for each of these methods.
MongoDB is extremely well documented and pretty much anything you want to do with it is explained really
well through the use of graphics and examples in their documentation.
So now it's your turn to create a new document using the Mongo shell and you're going to create the
second product in our products collection which is the pencil product.
Pause the video now
and in your Mongo shell create the second product.
OK so we're going to follow the format that we had previously.
We're going to tap into our database,
we're going to look for the collection that's called products and then we're going to again insert one
product. And that product is going to be again a Javascript object and it's going to have an id of 2
this time because this is the second entry.
And then it's going to have a name of pencil and it will have a price of 0.80, so 80 cents or
80p.
And when we hit enter, then we've created our second document.
So now we have a database called ShopDB that contains a single collection called products
and inside this collection is two documents one with this data and another with this data.