Thursday, June 4, 2015

Shell is on the way !!!

Now, that's all set. You've mongo DB Server installed, classpath is set and all the basic setup stuffs are working fine.

No, I've not done anything.
Well, you can do so very easily with the help of Mongo DB post Installation guide. If you need any help, you can go through the process.

Now, let's take a close look at each part. Hmm, So, it has two parts, a Server and a client interface. As a developer we are less bothered about the Server jobs and other DBA related stuffs.
So, let's go for the client part.

While installing MongoDB, we found something naming mongo.

Now, question arises what mongo is and why do we need that ?
Well, in a nutshell mongo is the shell client of MongoDB. This interactive JavaScript shell provides powerful interface for system administrators and developers. This shell provides a way to test queries and operations to the server without any specific client or driver.

Hmm, so it is somewhat analogous to Oracle SQL Developer ?
Well, yes. You can draw analogy between these two from a user point of view. However, mongo provides a fully functional JavaScript environment for MongoDB.

Hmm, so, how to use it ?
If you have set up your system with Mongo DB post Installation guide, simply open a command prompt, type mongo and hit enter. You will be connected to the shell client. This shell by default connects to the database hosted in localhost, port 27017 and test database.

What if I have another database to connect to ?
Hold on dear, I am gradually moving into the path.

OK go on. So, what is next ?
As MongoDB and RDBMS are different systems, so there terminologies are. Let's try to connect some dots between these two. If you are coming from a strong RDBMS backgrouond, the following can help you to get started. If you are new to databases, I recommend you to skip this section.

The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts.

SQL Terms/ConceptsMongoDB Terms/Concepts
databasedatabase
tablecollection
rowdocument or BSON document
columnfield
indexindex
table joinsembedded documents and linking
primary key
Specify any unique column or column combination as primary key.
In MongoDB, the primary key is automatically set to the_id field.
aggregation (e.g. group by)
aggregation pipeline
The following table presents some database executables and the corresponding MongoDB executables. We are making this short for our convenience.
MongoDBMySQLOracleInformixDB2
Database ServermongodmysqldoracleIDSDB2 Server
Database ClientmongomysqlsqlplusDB-AccessDB2 Client
more on this can be found here.

Well, at this point, you are connected to the database and you know some of the terminologies used in MongoDB world. Let's take another step.

You have a running database but you can not find any Collection or Document in the database. So, let's first create a document in the database.

Create a document in the database: To insert a document into the database we simply have to invoke a function insert. Let's first do that,

 db.inventory.insert(  
   {  
    item: "ABC1",  
    details: {  
     model: "14Q3",  
     manufacturer: "XYZ Company"  
    },  
    stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ],  
    category: "clothing"  
   }  
 )  

When you fire this command in the console and everything works fine, you are most likely to get something like below,
 WriteResult({ "nInserted" : 1 })  

What does that mean at all ?
Well, WriteResult is an object. nInserted is an attribute of WriteResult which determines the number of rows inserted into the database. If the insert operation had some errors, the WriteResult object would have contain the errors rather than the number of rows.

Wait, you mean to say that, we have inserted a row in a table without even mentioning the structure of the table ?
Well, you can tell that but there is a certain correction. The corrected phrase should be like,
We have inserted a document in a collection without even mentioning the structure of the collection.
Yes, we have done the same, we have inserted a record without doing anything. Well, if you take a look into the MongoDB introduction, you will get some more information.

Hmm, I am eager to see the same now.
Yeah, you are. Me was too !!!
Let's do the job then. Hit the following command,
 db.inventory.find()  

Voila, you get to see,

 { "_id" : ObjectId("53d98f133bb604791249ca99"), "item" : "ABC1", "details" : { "model" : "14Q3", "manufacturer" : "XYZ Company" }, "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ], "category" : "clothing" }  

Well, at this point we are going to get disconnected from the server and the client, but if you have grown some interest, I would like to recommend you the following. If you get time, go through it,
SQL to MongoDB

Bye for now

Palash Kanti Kundu

No comments:

Post a Comment