Image taked from

MongoDB The basic

This post is about MongoDB, the basic to install it and the basic to start to use it. Let’s begin!

MongoDB is at the top of the no relational database. It is written in c++ and work with javascript v8 engine.

To work with mongo you first should install it in your machine, so go to the next link and download it. Download MongoDB

After downloading mongo it is necessary to create a directory to save the databases. Give it all the permissions to this folder so mongo could save the data:

//You should be admmin for sure
$ sudo mkdir /data/db

Once you have downloaded it, it will create several files within the /bin directory, the most important to consider are the following:

Mongo: Is the shell to interact with the databases.
Mongod: Is the server.

Ok, now that you have installed mongo on your machine, you can start working with it.

First, start the server, open a terminal and run the following command:

$ mongod

It will start the mongo server and tell you that is waiting for connect. So in another terminal type the following to start the mongo shell.

$ mongo

Nice, so you are in the mongo shell now and ready to start working. Let’s start checking what we have within our recently installation.

> show dbs
test 0.078GB

By default mongo will select the test database, so to start, you should create a new database. For this example I will create a database to a soccer team. The command to do that is use, it will create the database if it doesn’t exist or select a database.

> use soocer_team
switched to db soccer_team

But, what if you don’t know what is your current database?

//This will show the current db you are in.
> db

Ok, now that we have the database created and selected, let’s create some collections. In Mongo, collections are the tables that we now from a relational database.

> db.createCollection("players")
{ "ok" : 1}

In the above command, the db select the current database and it works similar to the this keyword in javascript. You can be more specific when you create your collection and added more parameters to do a better configuration. I will not talk about that in this post, but if you want to learn more about it go to the Mongo documentation.

If you want to see what collection do you have in your current document type the following:

> show collections

Now, we have to insert data in our collection. Mongo have three methods to do it, insert() and insertmany(), and we will see how they work. To insert, query or update a collection you have to use db and the name of the collection you will work with.

> db.soccer_team.insert({"name": "Falcao", "lastname": "Rodriguez", "position": "forward", "number": 9, "age": 30})
WriteResult({ "nInserted" : 1 })

Now, we have our first player in our awsome team. Let’s create another one, this time with the save() method. Yes, with mongo you can create new fields in two different ways.

... "name": "Neymar",
... "lastname": "Santos",
... "position": "forward",
... "number": 7,
... "age": 25
WriteResult({ "nInserted" : 1 })

Ok, now that we have our talented forwards, lets create the rest of the team. Remember, you should have the mongo 3.2 version at least to use this command.

> db.players.insertMany(
"name": "Manuel",
"lastname": "Neuer",
"position": "Goalkeeper",
"number": 1,
"age": 31
"name": "Javier",
"lastname": "Mascherano",
"position": "Centre-back",
"number": 2,
"age": 33
"name": "Lionel",
"lastname": "Messi",
"position": "Forward",
"number": 10,
"age": 30

Excellent, now that we have some players in our team, lets find them.

> db.players.find()
{ "_id" : ObjectId("595d12fb0ce812c5792e4b19"), "name" : "Falcao", "lastname" : "Rodriguez", "position" : "forward", "number" : 9, "age" : 30 }
{ "_id" : ObjectId("595d13720ce812c5792e4b1a"), "name" : "Neymar", "lastname" : "Santos", "position" : "forward", "number" : 7, "age" : 25 }

Mongo will give you an unordered list of objects, it’s not to pretty(), right?

"_id" : ObjectId("595d12fb0ce812c5792e4b19"),
"name" : "Falcao",
"lastname" : "Rodriguez",
"position" : "forward",
"number" : 9,
"age" : 30
"_id" : ObjectId("595d13720ce812c5792e4b1a"),
"name" : "Neymar",
"lastname" : "Santos",
"position" : "forward",
"number" : 7,
"age" : 25

Oh, that’s much better! But what if we just want to check if we have some particular player only?

> db.players.find({"name": "Falcao"}).pretty()
"_id" : ObjectId("595d12fb0ce812c5792e4b19"),
"name" : "Falcao",
"lastname" : "Rodriguez",
"position" : "forward",
"number" : 9,
"age" : 30

Well, for now it’s everything. You now know how to to the basic configuration, how to start the Mongo server, how to select and create databases and how to insert and query collections. Of course this is a basic attempt to understand how easy is Mongo to learn and encourage you to navigate with this awesome technology.

You now have some material to start playing with Mongo. Have fun. :)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.