Sunday, November 29, 2015

Working with the shell - Javascript

In our previous post on creating collections, we used mongo client which is a shell client. This client is an interactive JavaScript Interpreter. So, you can write your JavaScript program and see the result immediately.

OK, let me prove my points. Let's start with running the server and the shell.

On two different Command Prompt start the following commands,

mongod

mongo

Now, you are going to see something similar to the following on the client,
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Palash>mongo
2015-11-29T13:53:42.288+0530 I CONTROL  Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.0.1
connecting to: test
>

OK, that's it. You have successfully started the client shell. Now, let's run a simple JavaScript program on it.
> for(i=0;i<5;i++){print("Hello World");}

On running this program, we get to see the following output, which is very obvious,
Hello World
Hello World
Hello World
Hello World
Hello World
>

It was fairly a simple program in JavaScript. Now, let's define some variables in the shell,
> x=1
1
> y=2
2
> z=5
5
>

You can see that, when a variable is assigned a value, the value is printed on the next line. Now et's try to query back the variable. We can do that simply by putting that variable name on the shell and the expression will be evaluated and the corresponding value will be returned. Let's try that,
> x
1
> y
2
> z
5
>

In similar fashion, we can define JSON objects. Here is an example,
> doc={name:"Palash"}
{ "name" : "Palash" }

Now, let us try to use the defined variables in a program,
> for(var i=0;i<z;i++){print(JSON.stringify(doc));}
{"name":"Palash"}
{"name":"Palash"}
{"name":"Palash"}
{"name":"Palash"}
{"name":"Palash"}
>

In our next example, we are going to insert lots of document in our database. Well, we'll be forming the documents with the iterator value as _id. Let's write a short JavaScript program which will create 9999 documents in a collection named huge.

But first let's check the database with 'show tables', if there is any collection with the name specified.
> show tables
Employee
Employees
system.indexes
>

So, I found that, in my database there is no collection exists with that name. If in case you are having a collection with that name, please specify other name for the following test.
> length=9999
9999
> for(var i=0;i<length;i++){
... doc = {_id:i, name:"developer"+i};
... db.huge.insert(doc);
... }
WriteResult({ "nInserted" : 1 })
>

After this program runs successfully, let's go back to the database and check the integrity,
> db.huge.count();
9999
>

So, we can see that, the new collection holds 9999 documents. Now let's check some of the documents,
> db.huge.find();
{ "_id" : 0, "name" : "developer0" }
{ "_id" : 1, "name" : "developer1" }
{ "_id" : 2, "name" : "developer2" }
{ "_id" : 3, "name" : "developer3" }
{ "_id" : 4, "name" : "developer4" }
{ "_id" : 5, "name" : "developer5" }
{ "_id" : 6, "name" : "developer6" }
{ "_id" : 7, "name" : "developer7" }
{ "_id" : 8, "name" : "developer8" }
{ "_id" : 9, "name" : "developer9" }
{ "_id" : 10, "name" : "developer10" }
{ "_id" : 11, "name" : "developer11" }
{ "_id" : 12, "name" : "developer12" }
{ "_id" : 13, "name" : "developer13" }
{ "_id" : 14, "name" : "developer14" }
{ "_id" : 15, "name" : "developer15" }
{ "_id" : 16, "name" : "developer16" }
{ "_id" : 17, "name" : "developer17" }
{ "_id" : 18, "name" : "developer18" }
{ "_id" : 19, "name" : "developer19" }
Type "it" for more
>

So, we can see that only the first 20 documents have been returned by the command and in the end, it has provided some instruction to key in "it", let's try that one. Let's see what happens when key in "it"

> it
{ "_id" : 20, "name" : "developer20" }
{ "_id" : 21, "name" : "developer21" }
{ "_id" : 22, "name" : "developer22" }
{ "_id" : 23, "name" : "developer23" }
{ "_id" : 24, "name" : "developer24" }
{ "_id" : 25, "name" : "developer25" }
{ "_id" : 26, "name" : "developer26" }
{ "_id" : 27, "name" : "developer27" }
{ "_id" : 28, "name" : "developer28" }
{ "_id" : 29, "name" : "developer29" }
{ "_id" : 30, "name" : "developer30" }
{ "_id" : 31, "name" : "developer31" }
{ "_id" : 32, "name" : "developer32" }
{ "_id" : 33, "name" : "developer33" }
{ "_id" : 34, "name" : "developer34" }
{ "_id" : 35, "name" : "developer35" }
{ "_id" : 36, "name" : "developer36" }
{ "_id" : 37, "name" : "developer37" }
{ "_id" : 38, "name" : "developer38" }
{ "_id" : 39, "name" : "developer39" }
Type "it" for more
>

Well, now we see next 20 documents on the screen and the same instruction to type in "it".

Actually, if you continue to type in "it" you will get to see all the documents until the total result set gets displayed.

OK, we'll discuss this particular topic on some subsequent discussion. Let's get back to the Shell.

So, we can see that the shell can interpret any javascript program. This can be handy for our use and even we have used this feature to create a huge collection. Huge collections will be proved to be useful when we we'll be going through our subsequent discussions on performance.

Well, that is just one of the most beautiful feature mongo client offers to us. here are some more features, that we can use. Take some time to go through the following video,


Hope, you have got a basic understanding of the features mongo client provides. In our next post, we'll be going through, the internal data representation of MongoDB, known as BSON.

Palash Kanti Kundu

No comments:

Post a Comment