Salem's Euphoria

Sharing Experience

A very short introduction to MongoDB

Leave a comment



Originally called humongous (big, enormous), the MongoDB project is carried by Eliot & Dwight since 2007. This is one of the best NoSql concepts implementation. For SQL fanatics, there is a small comparison between the SQL and the MongoDB NoSQL implementation.

NoSQL does not stand for “NO TO SQL” or “NON SQL”. It stands for “Not Only SQL”.

Recently I was testing it and I decided to pin this post for me and my team as a reminder.

MongoDB does not offer any official GUI. They delegated the mission to the community, and I think it was the right choice as there are now many GUI tools developed by the growing MongoDB community.

You can download the latest version from the Install it as an administrator if you are on Windows.

The default datastore for MongoDB is c:\data\db. Alternatively, you can specify the datastore when starting the DB engine (–dpath=”yourpath”).

To start the MongoDB server, run “mongod” on CMD. By default, 27017. You can check if the server was started by taping this address in the browser http://localhost:27017/. You will get this message on the page (It looks like you are trying to access MongoDB over HTTP on the native driver port.)
Open another console, and run “mongo.exe”.  First thing you must be aware of, there’s no “CREATE DATABASE” command on MongoDB. Let’s now create a new database called “knw”.  We’ll create a “table” called “projects” inside this database. A table in the MongoDB terminology is called a collection. A collection is a set of “rows” (called documents in this NoSQL variant). We’ll back to collections later in this post.

 MongoDB shell version: 2.6.3
 connecting to: test
//something like mysql use to specify current database
 > use knw 
 switched to db knw
 > show dbs;
 admin (empty)
 local 0.078GB
//there are no custom databases yet
//now, we'll insert a document in the table projects (not yet created)
 > db.projects.insert({name:"DALEEL", 
                  description:"Directories and VAS plateforms for call centers", 
 WriteResult({ "nInserted" : 1 })
 > show dbs
 admin (empty)
 knw 0.078GB
 local 0.078GB
//You can see that knw database was created

So, the database “knw” and the collection “projects” were created on first document insertion. Note that a collection can contain many documents with different structure.

> db.projects.insert({client:"Currency Museum", 
                      description:"Currency Museum management platform"})
WriteResult({ "nInserted" : 1 })

As you can see, column (field) names are not the same (“client” field does not appear in the first document although they are stored in the same collection).

Note also that a field value can be an integer, a string, a date  (etc) or a full document. (like “soldTo” field)

> db.projects.insert({name:"GEDARC", 
                      description:"EDM and ARCHIVES MGMT platform", 
                               name:"Client A", 
                     } )
WriteResult({ "nInserted" : 1 })

The select of MongoDB is called “find”. To find our main project “DALEEL”.

> db.projects.find({name:"DALEEL"})
{ "_id" : ObjectId("547d8321a2fdc9b65ab4805a"), "name" : "DALEEL", "description" : "Directories and VAS plateforms for call centers", "start_date" :
"2008-07-01" }

Now, you can start reading about NoSQL and MongoDB. There is something really humongous about MongoDB: The documentation 😉

One more post, I want you all to check before starting any project based on NoSQL spec, Sarah Mei arguing “Why you should never use MongoDB“.

How a NoSQL DB looks like in real world ? :D

How a NoSQL DB looks like in real world ? 😀


Author: Salem Ben Afia

Big Data & Java developer Search Engine Architect, Lucene Expert

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s