MongoDB – Part I

What is MongoDB ?

In short, extracted exactly from MongoDB site, MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.

Document Database

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects.

Advantages of using documents:

  • Documents (i.e. objects) correspond to native data types in many programming language.
  • Embedded documents and arrays reduce need for expensive joins.
  • Dynamic schema supports fluent polymorphism.

High Performance

MongoDB provides high performance data persistence.

  • Support for embedded data models reduces I/O activity on database system.
  • Indexes support faster queries and can include keys from embedded documents and arrays.

High Availability

MongoDB provides:

  • automatic failover.
  • data redundancy.

throught replica set. A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability.

Automatic Scaling

MongoDB provides horizontal scalability as part of its core functionality.

  • Automatic sharding distributes data across a cluster of machines.
  • Replica sets can provide eventually-consistent reads for low-latency high throughput deployments.


I will not dig into this task, you can find all the required information here for your preferred OS.

First steps

Once installed you must used it !! So let’s start with some basic command to check installation and become familiar with MongoDB.
I’ll use the mongo shall tool to connecto to MongoDB server and execute commands.

We can connect to MongoDB server starting mongodb shell, by default it lookps for a server listening at port 27017 on the localhost interface. You can connecto to a different port and host providing the –port and –host parameters.
Once connected you can execute:

  • db command to report the name of the current database
  • show dbs to display the list of databases
  • use <db name> to switch to the <db name> database

You must be aware that MongoDB does not create a database until some data is inserted.

One of the most useful function is help, it can be used :

  1. alone to get a quick informative page on how help command can be used
  2. as fisrt command followed by connect, keys, etc … to describe each command
  3. as method like .help() appended to some javascript, cursor, db and db.collection to get additional information

Let’s create our first document into MongoDB; first of all check with db command that we are using the mydb database, if not execute the command use mydb. Create two documents a and b like

a = { "firstname" : "davide", "lastname" : "brambilla }
b = { "x" : 3, "y" : -1 }

next step is to insert these document into the testData collection (we will see soon what a collection is) as follow

db.testData.insert( a )
db.testData.insert( b )

as mentioned before, at first insert both the database and the collection is created. Last steps are to confirm that testData collection exists and documents have been added to this collection, we can execute

show collections: which shows all the available collection in the database
db.testData.find(): which returns the set of document contained into testData collection
Posted in MongoDB

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: