0:03
Now that we have a had a brief introduction to MongoDB,
it's time to get hold of MongoDB,
install it on our computer,
and then interact with the MongoDB server.
To get started, of course the first thing is to install MongoDB on your computer.
To install MongoDB on your computer,
go to MongoDB.com, and on the right hand side here,
you will see the download button.
You can click on the download button in order to obtain the links to that download.
So when you click on the download button,
you'll be taken to the download center where you would
see various versions of MongoDB available to us,
or you can access it online using database as a service.
But in our case we're going to download MongaDB and install it on our computer.
So to do that we will choose the community server version of MongoDB,
and when you do that you will be automatically given access to
their specific version suited for your computer platform.
So in this case I'm going to download their OS X version of the MongoDB,
and then do the installation.
If you are using a Windows machine,
then you can download the Windows Installer.
For a Linux machine correspondingly,
you can download the corresponding installer here.
To install MongoDB, the MongoDB
documentation itself provides you with detailed instructions.
I don't think I can give you any better instructions than what is already provided there.
So to understand how to install MongoDB on your computer,
head over to docs.mongodb.com/manual/installation,
and then click on the Install MongoDB Community Edition.
And here you will see the installation instructions for various platforms.
So to install on Windows,
click on the install on Windows and then go through the steps given here,
and they specify how to download MongoDB and
then install the MongoDB community edition on your computer.
So go ahead and follow these instructions to install MongoDB on your computer.
Also they tell you how to get started with MongoDB.
Now once you have completed the installation,
you can proceed ahead to the next step that we give in the exercise.
Similarly, if you are installing on OS X,
just follow the instructions given here to install it on OS X machine,
and also correspondingly for the Linux platforms.
I assume by now you have installed MongoDB following
the instructions given in the documentation.
Then once you have completed that,
go to a convenient location on your computer.
So here I am in my Coursera folder.
I'm going to create a new folder here named MongoDB.
And then inside the MongoDB folder,
I will create a new folder named Data.
Now this data folder is where MongoDB will store
all the data relevant for my current project.
Once you have created these folders,
open a command window or a terminal at the MongoDB folder.
So here I have my terminal and I'm currently in the MongoDB folder,
that I had just created.
So at the prompt,
in order to start the Mongo server type mongod --dbpath=data.
Recall that we already have the data subfolder inside the MongoDB folder.
So the DB path here essentially takes the path
to the folder which will store the data for my Mongo application.
Now you can execute this mongod command from any location on your computer as long as you
specify the complete path to the location of
the data folder where your MongoDB data is going to be stored.
Since I am already in the MongoDB folder,
and the data folder is a subfolder of the MongoDB folder,
I can just simply execute the command by saying mongodb --dbpath=data.
This should start up your MongoDB folder here.
And if it asks for some permissions,
just allow MongoDB to be available for being connected to from different locations.
Open another terminal window or a command prompt,
and at the prompt type mongo in order to start
the Mongo REPL or Mongo read evaluate print loop.
So this is a command line way of accessing your Mongo server.
So when you type Mongo,
it will automatically connect itself to the MongoDB server that is
running on your computer at the port number 2 7 0 1 7.
So here you see this is automatically connecting.
So when you start the mongo REPL,
it'll know exactly where to look for
the default MongoDB server which will be running at port number 2 7 0 1 7.
In this course, we are going to be running the MongoDB server at its default port number,
and so that it's easy to access that.
Also on OS X machine,
you might see a warning like this which says "soft rlimits too low".
You can just ignore that warning.
Everything will work just fine,
even though you'll see that warning being printed there.
This has something to do with the amount of
memory space available for your Mongo process,
and this will be just fine to run for
the development version of the Mongo that we are going to be using in this course.
Now once the Mongo REPL starts,
you will see a prompt,
and at the prompt you can type various commands to access your Mongo server.
So at the prompt if you type db,
it will specify the specific database
on the Mongo server to which it is currently connected.
So now we're going to create a new database there with the name conFusion.
So to create a new database,
I will just say use conFusion at the prompt.
So this will automatically create a new database named conFusion,
if it doesn't already exist.
If it exists, then it will automatically switch to that conFusion database.
And thereafter, any commands that you execute will be accessing this conFusion database.
So at the prompt if you now type db,
you will see that it prints up saying conFusion.
The test that it printed earlier was a test database that is
automatically set up when you start running the Mongo server.
Now to learn the biggest commands that are supported,
you can just type db.help at the prompt,
and this will printout a set of commands that are available
to you at that Mongo REPL command prompt.
We're going to be using a few of these to access the MongoDB database.
Again typing db, you see that we are accessing the conFusion database.
Let's create a new collection in this confusion database.
To do that, at the prompt type db.dishes.
Now the dishes here refers to a collection.
Now if this collection already exists,
then this will access the dishes collection.
If it doesn't exist, then it will create the dishes collection at this point.
So in the dishes collection,
I'm going to insert.
So this is where we use the insert function that is available on the collection here.
And then let's insert a record into this collection or a document into this collection.
So at the prompt, I will type name,
8:51
I am just typing some random stuff here,
just to show you that we can insert a document into the database.
So with this, this particular Json document that you see here,
will be automatically inserted into the database.
So the insert function takes a Json document as its parameter y,
and immediately the Mongo REPL will respond saying right result,
number inserted is one.
So one record has been inserted,
one document has been inserted into this collection.
Now to printout the various documents in this collection,
we can simply say db.dishes.find.
So the Find function will find all the documents in this collection,
and it'll print out those documents.
Now, if I just say find,
then it will printout this information like this here,
as a single line and you can see that this is a Json string here.
Now we can also use the pretty function,
that is available, which will print out the documents in a more easily readable manner.
So if you type db.dishes.find().pretty();
So you see that the document that we had inserted earlier is now seen here.
So you can see that the name and the description that we had
inserted is available in this document.
In addition, note how MongoDB has automatically added an _id field to this document.
So if you don't explicitly specify the _id field,
MongoDB will automatically add the _id field.
And as I explained,
in the lecture earlier,
this is of the type ObjectId,
and you can see that this string specifies the ObjectId information.
Now this ObjectId, as we learned in the lecture earlier,
has a specific fall back,
which also includes the time stamp, the machine name,
the processId, and also the increment value that is stored here.
So now similarly if we want to be- I can simply create an object id using the Mongo REPL.
So let me just type var id =
new ObjectId() just to
demonstrate to you that the objectId will contain the time stamp.
So here I have created this variable named ID which is a new object type.
Now I can simply type Id.getTimestamp();.
You will notice that this will printout
the current time value in the ISO format, as you see here.
So this shows you that the object ID actually contains the timestamp field here.
So similarly you can ask for the other fields also from the object id.
And to quit the Mongo REPL,
just type exit at the prompt.
And the Mongo REPL will exit and then bring you
back to your terminal prompt or your command prompt.
With this, we complete this exercise.
In this exercise, we have seen how we download and install MongoDB on our computer,
how we start up a MongoDB server,
and then access the MongoDB server using the Mongo REPL and interact with the server.