Installing MongoDB

Today, we are going to install MongoDB in our system. In my case, I am installing it in a Mac OS X system but, the steps are almost the same in other systems.

The first thing to do, it is to go to the MongoDB download web page.

Here, we can find two different download:

  • MMS: The Easiest Way to Run MongoDB. We DO NOT need to download this one. It is to manage instances in the cloud.
  • Download and Run MongoDB Yourself. This is the one we are going to download. In this moment, the production version is the 3.0.1. We only need to download the adequate file for our system.

The next step is to extract the file. For this, we can use the command:

tar xvf mongodb-<version>.tgz

Now, MongoDB is installed in our system. But, let’s try if it is working. For this, we are going to use two commands in the /bin folder inside the MongoDB main folder:

  • mongod: It is the daemon that it is going to start our db.
  • mongo: It is the shell command to connect to our instance of MongoDB.

In this point, you have two options to start your MongoDB instance:

  • Start your MongoDB instance as root.
    1. Create the folder /data/db to store the data from MongoDB.
    2. Execute the mongod command.
  • Start the MongoDB instance in a custom path and using a normal user (I prefer this one)
    1. Create a folder wherever you want to store the data from MongoDB.
    2. Execute the mongod command with the option –dpath “dataFolder”. In this case, the MongoDB data is going to be stored in you custom folder and not in the default folder.
./mongod --dpath "/Volumenes/HD/mongodbdata"

After choose an option and run the daemon command, our database should be running. Let´s now try to connect and test if everything is working fine.

Let’s insert a new document:

Input  -> db.persons.insert({'name':'John'})
Result -> WriteResult({ "nInserted" : 1 })

Let’s check the documents in persons schema:

Input  -> db.persons.find()
Result -> { "_id" : ObjectId("550899e204f790a330f343b0"), "name" : "John" }

That’s all. Now, we only need to start to play with MongoDB.

See you.

Note: In the download page, there is a section call “Packages” where you can find MongoDB available for some package managers.

Installing MongoDB