Wednesday 27 August 2014

mongoDB: Lesson2 : Creating Collection & Dropping Collection.

Hey Guys,
The fun continued here with mongodb next topic, creating collections(tables in RDBMS) and inserting documents(rows in table in RDBMS).



#
 Task
Syntax with Example




1




Collection








2




Creating Collection



Method -1 :


By using createCollection method
Using db.createCollection(name,options) function


Syntax: db.createCollection(<name>,<options>)

In the above syntax <name> is the name of the collection & options are various other parameters like capped, size, max and etc.(Refer official documentation for this).

Example 1: Just creating a collection Name

> db;
test

> db.createCollection("myFirstCollection");
{ "ok" : 1 }

> show collections;
myFirstCollection
nettuts
system.indexes
>

Example 2: Creating collection with along with document.

> db.mySecondCollection.save({"firstName":"sadakar","lastName":"pochampall",ID:200});
WriteResult({ "nInserted" : 1 })

> db.mySecondCollection.save({"firstName":"dolly","lastName":"boray",ID:201});
WriteResult({ "nInserted" : 1 })

> db.mySecondCollection.find();
{ "_id" : ObjectId("53fdbdbe268aa77e481c4a6c"), "firstName" : "sadakar", "lastName" : "pochampall", "ID" : 200 }
{ "_id" : ObjectId("53fdbde4268aa77e481c4a6d"), "firstName" : "dolly", "lastName" : "boray", "ID" : 201 }
>









3






Method -2:


By using insert method:
  • Insert method can be used in two ways.
    • Create documents separately and insert them into collection.


Create documents separately and insert them into collection.
Example:
> i={"firstName":"sadakar","lastName":"pochampalli",id:100};
{ "firstName" : "sadakar", "lastName" : "pochampalli", "id" : 100 }

> j={"firstName":"dolly","lastName":"boray",id:101};
{ "firstName" : "dolly", "lastName" : "boray", "id" : 101 }

> db.myFirstCollection.insert(i);
WriteResult({ "nInserted" : 1 })

> db.myFirstCollection.insert(j);
WriteResult({ "nInserted" : 1 })

> show collections;
myFirstCollection
nettuts
system.indexes

> db.myFirstCollection.find();
{ "_id" : ObjectId("53fdbaad268aa77e481c4a6a"), "firstName" : "sadakar", "lastName" : "pochampalli", "id" : 100 }
{ "_id" : ObjectId("53fdbab2268aa77e481c4a6b"), "firstName" : "dolly", "lastName" : "boray", "id" : 101 }
>







References :


http://docs.mongodb.org/manual/tutorial/getting-started/
http://docs.mongodb.org/manual/reference/method/db.createCollection/
http://docs.mongodb.org/manual/reference/method/db.collection.insert/





4

Drop Collection
Syntax: db.<CollectionName>.drop();


Example:
> db.myFirstCollection.drop();
true
>


:-)

mongoDB: Installation of mongoDB in Cents OS 6.5 final(32 bit)

Hello Everyone,

This article is specific installation of mongodb in  32 bit Cent OS 6.5 final.


#
Task to Perform
Implementation




1




Set Repository for mongoDB
 
  • Open the terminal with su permission.
  • You need to create mongdb.repo file at /etc/yum.repos.d/
  • i.e.,
  • [root@localhost yum.repos.d]# vi mongdb.repo
  • Write below content in the file

           [mongodb]
            name=MongoDB Repository
            baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
            gpgcheck=0
           enabled=1

  • Save the file (Esc+q!)

         NOTE:
          The above set up is for 32 bit Cent OS.

          For 64 bit OS : Refer official documentation and do follow the steps
         described here in the same fashion.


2


Installation Command

  • You should be on terminal opened with su credentials.
  • Issue yum install mongodb.org command ]
  • i.e., [root@localhost]# yum install mongodb.org
  • Above command will install the latest release of mongoDB.


3


Start the mongoDB Server


     [root@localhost mongo]# service mongod start
       Starting mongod: [ OK ]
       [root@localhost mongo]#


     NOTE:
       I have observed Startup of the server failed.
     * Delete mongod.lock file located at var/lib/mongo and start the server



4

Check whether the server started or Not in logs

  • You need to check the mongod.log file located at /var/log/mongodb/mongod.log

5

Stop the mongoDB Server

    [root@localhost mongo]# service mongod stop
     Stopping mongod: [ OK ]
     [root@localhost mongo]#



6

Check the status of the Server

   [root@localhost mongo]# service mongod status
    mongod (pid 4849) is running...
    [root@localhost mongo]#

7

Auto Start of mongoDB with system boot

   Issue below command to auto start the mongoDB whenever you reboot
   the system .
   #chkconfig mongod on



8

Check mongoDB version

  [root@localhost /]# mongo -version
   MongoDB shell version: 2.6.4




9


Restart the mongoDB Server


     [root@localhost /]# service mongod restart
      Stopping mongod: [ OK ]
      Starting mongod: [ OK ]




10


Jump to start mongo shell to get start with scripting/querying etc



Simply type mongo on the terminal

For example:
[root@localhost /]# mongo
MongoDB shell version: 2.6.4
connecting to: test
Server has startup warnings:
2014-08-27T12:14:05.196+0530 [initandlisten]
2014-08-27T12:14:05.196+0530 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2014-08-27T12:14:05.196+0530 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
2014-08-27T12:14:05.196+0530 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
2014-08-27T12:14:05.196+0530 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
2014-08-27T12:14:05.197+0530 [initandlisten]
>

11

List databases

> show dbs;
admin (empty)
local 0.078GB
>


MongoDB articles list in this site :
1) Getting Started with MongoDB - The Beginners Tutorial 
2) mongoDB-->Tip 1: Clear the mongoDB consloe : mongo editor clear screen command 
3) mongoDB : Lesson 1 : Creating /dropping databases in MongoDB

:-)



Thursday 14 August 2014

mongoDB : Lesson 1 : Creating /dropping databases in MongoDB

Hi Guys,

Fun started here with mongoDB...

1) What is mongoDB database ?
2) How to create mongoDB database ?
3) How to drop mongoDB database ?

mongoDB database :
  • A physical container for collections.[where as in RDBMS it's a collection of related tables]
  • Each database gets its own set of files on the file system. 
  • A single MongoDB server typically has multiple databases.

Lesson 1 : Creating /dropping databases in MongoDB

S.NO
Task To Perform
Command
Sample Output

1

List  all the databases

>show dbs

> show dbs;
admin  (empty)
local  0.078GB
2
Creating Database
Syntax:
>use <databaseName>
Example:
>use myFirstTestDB
> use myFirstTestDB
switched to db myFirstTestDB
3
Check the current database
Syntax : >db
> db;
myFirstTestDB
4
Again list all the databases
>show dbs
> show dbs;
admin  (empty)
local  0.078GB

NOTE :
·         Until and unless you insert some documents into the database by creating a collection you will not see the created database in step 2.
·         To make sure database created you need to create a collection(table in RDBMS)and insert at least one document(records or rows in RDBMS) in it.
5
Insert documents into a collection of database
Syntax :
db.<collectionName>.insert({
key1: value1,key2:value2;
key3:value3
})
>db.student.insert({name:"sadakar",state:"telangana",city:"hyderabad"});
WriteResult({ "nInserted" : 1 })
NOTE:
student is the collection(table in RDMS) name and script between {} is a document(row in RDBMS)

NOTE :
1) Once you press enter button the mongod will create myFirstTestDB database, student collection(table in RDBMS) with the document (rows in RDBMS)permanently
2) There are some other ways to create documents and insert into collections of databases which is not explained in this example.  
6
Now list the all the databases
>show dbs;
> show dbs;
admin          (empty)
local          0.078GB
myFirstTestDB  0.078GB
7
Dropping database
Syntax:
>db.dropDatabase()


NOTE:
1) List databases
2) check current database
3)issue drop function
4) again list the databases
> show dbs;
admin          (empty)
local          0.078GB
myFirstTestDB  0.078GB

> db;
myFirstTestDB

> db.dropDatabase();
{ "dropped" : "myFirstTestDB", "ok" : 1 }

> show dbs;
admin  (empty)
local  0.078GB



Learn Quickly by reading my articles and get back to me in comments if you face any issues :-)

Sadakar
BI developer

mongoDB-->Tip 1: Clear the mongoDB consloe : mongo editor clear screen command

Hi Guys,
Here we go,
  • We write scripts on mongo shell console.
  • If we want to clear the console we have to use below command on the mongo console.
  • There are two commands to clear. 
    • cls command (or)
    • Ctrl+l (clear screen)
  • Both the commands clear  the screen and put the cursor position on top. 
  • We can use keyboard Up and Down buttons to see the buffered commands.(i.e., previously written commands).
Reference : http://stackoverflow.com/questions/13327218/how-to-clear-console-in-mongodb


Sadakar
BI developer

Jasper CE+EE/Pentaho CE/Talend CE/Kettle CE/databases/mongoDB(Learning).

Wednesday 13 August 2014

Getting Started with MongoDB - The Beginners Tutorial

 In this article, I'll be explaining the basics to start with mongoDB.
  1. What is mongoDB ?
  2. Installation of mongoDB in Windows 2008 64 bit OS.
  3. Start the mongoDB Server ?
  4. What is the client Tool for mongDB Server ?
 1. What is mongoDB ?

  • MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.
  • MongoDB works on concept of collection and document.
  •  More information : 1) http://docs.mongodb.org/manual/core/introduction/ 
  • It is a Big Data Technology. i.e., noSQL database. 

2. Installation of mongoDB in Windows 2008 64 bit OS ?
  •  Click on Download, by default, you will get msi file.
  • Once the download completes double click on the .exe file to get finish the installation.
  • Installation will go under below folder : C:\Program Files\MongoDB 2.6 Standard.
  • NOTE : I have not explained log file set up in this article.
3. Start the mongoDB Server ?
  • There are couple of ways(setting up windows services) to start the mongoDB Server in windows. Here I have described a simple way of starting the server from command line.
  • It's always best practice to start the server(any server) from command line(Why? logs we can see on the command line console). 
  • You need to run the mongod.exe file from command prompt opened with Admin privileges.
  • MongoDB requires a data folder to store its files.
  • The default location for the MongoDB data directory is c:\data\db. 
  • You can also create data directory any where in your hard drive : for example it could be D:\data\db
  • NOTE : This folder we have to create explicitly. (Either using New Folder GUI in windows or use command prompt)
  • Use the below command to Start the mongoDB server. 
  • C:\Program Files\MongoDB 2.6 Standard\bin>mongod.exe --dbpath D:\data\db
  • NOTE :

Friday 8 August 2014

Tip : Take backup of postgresql database from command prompt - Postgeres of jasperserver installed in Cent OS 6.5 final

Hi Guys,

Below steps will be followed for taking back up of a database from command line for postgresql database.

1) Go to installation folder of postgres (postgres installed with jasper server).

2) Go up to the bin folder & and open the location with terminal with su(superuser) credentials.
 (You can find file names like pg_dump, pg_dumpall and etc)

3)Execute below command for taking backup of particular database.

Let us assume student is the database we would want to take back up.
& the output .sql script file name is : student.sql( can give anyname).

[root@localhost bin]# ./pg_dump -U postgres -W -F p student >  /home/sadha/student.sql

In the above command:

U = UserName
W=Prompt for password once you press enter
F=File Format
p=plain .sql file
t=tar file

Reference :
Backup:

http://www.postgresqltutorial.com/postgresql-backup-database/

Restore:
http://www.postgresqltutorial.com/postgresql-restore-database/


Thank you.
Sadakar
BI developer