Sunday, August 23, 2015

Working with the shell - Create Collection, Insert Document

Now, you have installed MongoDB, you have the shell ready to use. Let's do some basic operations.

First, start the MongoDB server.
Open command prompt/shell and run the following command,
1
mongod

When you run the command, you are likely to see the following output,
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
2015-08-23T12:36:06.334+0530 I CONTROL  Hotfix KB2731284 or later update is not installed, will zero-out data files
2015-08-23T12:36:06.393+0530 W -        [initandlisten] Detected unclean shutdown - C:\data\db\mongod.lock is not empty.
2015-08-23T12:36:06.423+0530 I JOURNAL  [initandlisten] journal dir=C:\data\db\journal
2015-08-23T12:36:06.438+0530 I JOURNAL  [initandlisten] recover begin
2015-08-23T12:36:06.459+0530 I JOURNAL  [initandlisten] recover lsn: 55843
2015-08-23T12:36:06.459+0530 I JOURNAL  [initandlisten] recover C:\data\db\journal\j._0
2015-08-23T12:36:06.484+0530 I JOURNAL  [initandlisten] recover skipping application of section seq:0 < lsn:55843
2015-08-23T12:36:06.585+0530 I JOURNAL  [initandlisten] recover cleaning up
2015-08-23T12:36:06.585+0530 I JOURNAL  [initandlisten] removeJournalFiles
2015-08-23T12:36:06.588+0530 I JOURNAL  [initandlisten] recover done
2015-08-23T12:36:06.672+0530 I JOURNAL  [durability] Durability thread started
2015-08-23T12:36:06.833+0530 I CONTROL  [initandlisten] MongoDB starting : pid=2100 port=27017 dbpath=C:\data\db\ 64-bit host=Palash-PC
2015-08-23T12:36:06.833+0530 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2015-08-23T12:36:06.834+0530 I CONTROL  [initandlisten] db version v3.0.1
2015-08-23T12:36:06.834+0530 I CONTROL  [initandlisten] git version: 534b5a3f9d10f00cd27737fbcd951032248b5952
2015-08-23T12:36:06.834+0530 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1j-fips 15 Oct 2014
2015-08-23T12:36:06.835+0530 I CONTROL  [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2015-08-23T12:36:06.835+0530 I CONTROL  [initandlisten] allocator: system
2015-08-23T12:36:06.835+0530 I CONTROL  [initandlisten] options: {}
2015-08-23T12:36:06.992+0530 I JOURNAL  [journal writer] Journal writer thread started
2015-08-23T12:36:08.557+0530 I NETWORK  [initandlisten] waiting for connections on port 27017
2015-08-23T12:36:16.464+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53085 #1 (1 connection now open)
At this point, MongoDB is running on localhost (127.0.0.1) on port 27017 by default. We'll see later how to change these defaults.

Now the server is up and running and ready to accept connections from clients.

Have you noted something ?
Don't you think, it is really very fast at starting up and accepting connections ?

Which client ?
You can use any client, a Java client, a Python client, a .NET Client, a shell client or if you are crazy enough, you can write your own client.

For now, we'll just use the Mongo Shell client for our use.

How do I get that client ?
While installation, MongoDB provides this basic client. Just open another console and hit the following command,
mongo

This will also connect to localhost:27017 and default to test database and most likely to print similar to,
1
2
3
4
2015-08-23T12:36:16.370+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
>

That's it. Let's start doing some thing. Let's first see what are the collections already present. Hit either of the following,
1
show tables
or,
1
show collections

If you are using this for the first time, you'll find the following,
1
system.indexes

Otherwise, you can find other collections you might have created earlier.

Hold on, I've not created system.indexes.

Well, this collection is a system collection, which stores the details of the indexes created in the database.

OK, how do I create my own collection ?
The easiest way to create a collection is to create no collection at all.

What ? Have you lost your mind ?
I think, I am completely with my senses.
Actually, this is unbelievable at first that, you don't create a collection until you use it but this is true for MongoDB. You create your collection when you insert your first document in the collection. MongoDB implicitly creates the collection for you.

However, you can create your collection explicitly using db.createCollection() command, this is more likely to be a more advance topic which we can discuss later.

Cool !!!
Yup it is. Another more interesting thing to notice here is that, you can even query on a non-existing collection.

What ???
Perfectly true, let me show you an example.
Follow these steps,
  1. Run the show collections command
  2. You will see the existing collections
  3. Then query a non-existing collection.
That's how it looks like in the shell,
1
2
3
4
> show collections
system.indexes
> db.animals.find()
>

So, you see that, you don't have a collection named animals but you actually can query it using the find() method.
Well, as expected you don't have a record to return from the db. If it did that, then probably it would be a mess in the database and perhaps no one would have use it.

Hmm...(Aww...). Don't you think, it is a mess indeed. You are performing an operation which should not be performed.
Well. I don't want to debate on this topic. I just know that this is a feature that you can query non-existing collection with no output instead of getting exceptions. Actually, I personally like this eaturre. May be your choice is different.

Now, let's dive deeper. We've database server up and running, we are connected to the database via mongo shell. Now, let's try to store some information in the database.

If you think in the SQL perspective to store information you need to create a table first. But it is different in case of MongoDB as you might have already experienced.

So, the question arises, where to put the information. Well, we'll do that using Collections. But to get into the collection, you need some container to get handle of it.

It happens to be that, MongoDB provides us an object of the database to simplify our work and we can perform our operations using the database object. To get a hold on the database, we use db command in our shell. Let's try it,
>db
test

Which returns the name of the database, the shell is connected. As in my case, it returns test.

OK. So, how do I create a collection ?
Well, let's now move into it. Think of any collection name. Let's start with Employees collection.
1
db.Employees.insert({'sap_code': 51531642, 'organization': 'HCL Technologies', 'department': 'Development', 'role': 'Senior Software Engineer'});

And when run, it should print the following,
1
WriteResult({ "nInserted" : 1 })

Now again check collections,
1
show collections

And this time you should see the Employees collection.

1
2
Employees
system.indexes

So, let's try to understand what we've fired and what instruction was provided to the database.

As we've seen earlier, db returns the current database and we have used a collection on the db by using the dot operator. So, db.Employees actually represents the Employees collection in the current database. Now on that, we've invoked the insert operation. Which takes JavaScript object as argument.

In a nutshell, we've instructed the database server to add a document(JavaScript object) to the Employees collection in the current database. In the server side, it will check for the collection, if it is present, it adds the argument of the insert method to that particular collection. Otherwise, it creates the collection and adds the document to it.

That's it, you have successfully created a collection in the DB. Let's fetch the records from the collection Employees,

1
2
3
> db.Employees.find();
{ "_id" : ObjectId("55d97adafa54c8ea260b8288"), "sap_code" : 51531642, "organization" : "HCL Technologies", "department" : "Development", "role" : "Senior Software Engineer" }
>

As you can see, there is only one document available in the collection. But the format of the result is somewhat difficult to read. Let's make it pretty. Just add pretty() in the end.

1
2
3
4
5
6
7
8
9
> db.Employees.find().pretty()
{
        "_id" : ObjectId("55d97adafa54c8ea260b8288"),
        "sap_code" : 51531642,
        "organization" : "HCL Technologies",
        "department" : "Development",
        "role" : "Senior Software Engineer"
}
>

Now, that's cool to read. But hold on. I have not added that _id in the document.
Well, that's with the MongoDB. To insert a document in MongoDB, we need to have a unique identifier for each document. That's the primary key of the document.

In case if you have provided an _id value for the document, which is unique in the collection, it will use that value as primary key. However, in case if you have not provided _id, MongoDB will generate one for you. You can use anything as _id such as numbers, Strings, ObjectId etc. Another important point to discuss here is that the _id value is immutable. Once one document is created with an _id value, you cannot change it. Let's try adding one document with _id in the Employees collection.

> db.Employees.insert({ "_id" : 1, "sap_code" : 51534225, "organization" : "HCL Technologies", "department" : "Development", "role" : "Technical Lead" });
WriteResult({ "nInserted" : 1 })
>


The document is added to the Employees collection. Let's try to add the same document(with same _id) once again,


> db.Employees.insert({ "_id" : 1, "sap_code" : 51534225, "organization" : "HCL Technologies", "department" : "Development", "role" : "Technical Lead" });
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "E11000 duplicate key error index: test.Employees.$_id_ dup key: { : 1.0 }"
        }
})
>
>

This time you are getting the duplicate key error. It actually provides you the exact location of the error. In my case, it is showing error for test.Employees.$_id_.

Now let's query the database and see what happened to the Employees collection.


> db.Employees.find().pretty();
{
        "_id" : ObjectId("55d97adafa54c8ea260b8288"),
        "sap_code" : 51531642,
        "organization" : "HCL Technologies",
        "department" : "Development",
        "role" : "Senior Software Engineer"
}
{
        "_id" : 1,
        "sap_code" : 51534225,
        "organization" : "HCL Technologies",
        "department" : "Development",
        "role" : "Technical Lead"
}
>


Watch out for the cases in the query command. Cause, MongoDB shell is case sensitive as you can expect it from a JavaScript point of view. You can't see the expected result when you have missed out the case. For example, following query does not work,

1
2
> db.employees.find().pretty()
>

OK, let's add one more document to the Employee collection,

1
2
3
> db.Employees.insert({'sap_code':51534225,'name':'Monojit Das',  'organization':'HCL Technologies', 'department':'Development', 'role':'Technical Lead'})
WriteResult({ "nInserted" : 1 })
>

Let's query the collection again,


> db.Employees.find().pretty();
{
        "_id" : ObjectId("55d97adafa54c8ea260b8288"),
        "sap_code" : 51531642,
        "organization" : "HCL Technologies",
        "department" : "Development",
        "role" : "Senior Software Engineer"
}
{
        "_id" : ObjectId("55d98196fa54c8ea260b8289"),
        "sap_code" : 51534225,
        "name" : "Monojit Das",
        "organization" : "HCL Technologies",
        "department" : "Development",
        "role" : "Technical Lead"
}
{
        "_id" : 1,
        "sap_code" : 51534225,
        "organization" : "HCL Technologies",
        "department" : "Development",
        "role" : "Technical Lead"
}
>

Hey, you have just added one more column in the record and no error ?
Yes, I've added one more column attribute in the document. This is perfectly fine  with MongoDB, you can add or remove attributes to the document in the collection without affecting other documents in the collection. You can go through this article on Document Data Store to get this more clarified.


I hope, at this point, you are able to
  1. Create a collection
  2. Insert a document in the collection
  3. Find all the documents in the collection
  4. Show the results in a more readable fashion

Let's watch a small video depicting all the above information,



Next, we'll be going through more on inserting documents and shell.

Prev     Next
Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu Palash Kanti Kundu

No comments:

Post a Comment