Introduction:
MongoDB is open source document database and its NoSQL database. MongoDB implemented in C++.
Now a day’s NoSQL database become popular in the market.
We have two types of Databases
- SQL database
- NoSQL Database
SQL database
We already know about SQL databases in the market these are RDMS databases. We have many popular RDMS databases in the market such as Oracel, MySQL, DB2, PostgreSQL and SQL Server. RDMS databases are managed the data by using Tables. We store the data in tables.
To communicate with these RDMS databases we have common language called SQL. We will use SQL to access and insert data into tables. We have different relations and its mappings between tables to simplify the complex data storage.
Each database vendor have to provide different clients to connect to different applications and which are developed by different languages. Database vendor is responsible to provide language depended database drivers to connect to other applications with database.
We are talking here about java technology so all database vendors have to provide the java JDBC Driver library with these libraries we can connect to RDBMS databases.
NoSQL Database
NoSQL is one of the concepts to access database without any specific database dependent queries instead of that we will use simple programming based object accessing over the databases.
A NoSQL database provides a mechanism for storage and retrieval of data that employs less constrained consistency models than traditional relational databases. Motivations for this approach include simplicity of design, horizontal scaling and finer control over availability
We have many flavors in NoSQL databases
The following are some of the important types
Document databases
Document databases pair each key with a complex data structure known as a document. Documents can contain many different key-value pairs, or key-array pairs, or even nested documents.
Graph stores
Graph stores are used to store information about networks, such as social connections. Graph stores include Neo4J and HyperGraphDB.
Key-value stores
Key-value stores are the simplest NoSQL databases. Every single item in the database is stored as an attribute name (or "key"), together with its value. Examples of key-value stores are Riak and Voldemort. Some key-value stores, such as Redis, allow each value to have a type, such as "integer", which adds functionality.
Wide-column stores
Wide-column stores such as Cassandra and HBase are optimized for queries over large datasets, and store columns of data together, instead of rows.
More Information
MongoDB
MongoDB is one of the NoSQL database and it is document database.
Working with MongoDB
MongoDB database we can install in any Operating System and we can connect to database server and manage the data.
Install and Using MongoDB
The following are steps
- Download MongoDB Database
- Create MongoDB data Store Directory
- Create MongoDB log file
- Create MongoDB configuration file
- Start MongoDB server
- Connect and use MongoDB
Download MongoDB Database
MongoDB server available for all major operating system bases on our operating system we need to download MongoDB database server
The following is MongoDB server Download location
Windows 7
We have installation files and binary distribution, we can use any thing.
Note:
In this article we will install and using MongoDB for Windows 7 and 64 bit OS
Once we download the MongoDB binary distribution ZIP file we will place any one of the directory and need to extract the zip file.
Once you extract we can see bin directory there we can see many .exe file to perform different actions.
Assume we have extracted MongoDB Zip file in D:\MongoDB
The following is screen to show bin directory and exe files of MongoDB
Create MongoDB data Store Directory
We need create one directory there MongoDB will store data.
Assume we have created data directory in D:\MongoDB\data\db
Create MongoDB log file
We also need to create log file for MongoDB to store log information. We need create mongo.log file in any of the directory
Assume we have created mongo.log in D:\MongoDB\log directory.
Create MongoDB configuration file
MongoDB need one configuration file when server starts so we need to create mongo.configfile in any location in your machine.
In the configuration file we need to mention data storage path for MongoDB and need to specify the log file location.
We already created data store directory and log file in previous steps.
Create mongo.config file and add following configuration in the file
##store data here dbpath= D:\MongoDB\data\db ##all output go here logpath=D:\MongoDB\log\mongo.log ##log read and write operations diaglog=3 |
The following is sample mongo.config fine one of the directory
The following is screen shot which show required directories for MongoDB server
Important Direcory and files paths as for above assumptions
MongoDB Server Path D:\MongoDB\mongodb-win32-x86_64-2008plus-2.6.0 MongoDB Server Bin directory Path D:\MongoDB\mongodb-win32-x86_64-2008plus-2.6.0\bin MongoDB data store path D:\MongoDB\data\db MongoDB configuration file path D:\MongoDB\config\mongo.config MongoDB log file path D:\MongoDB\log\mongo.log |
Start MongoDB server
We have done all the steps and now we will start the MongoDB server
The flowing is command to start server
mongod --config d:\MongoDB\config\mongo.config --dbpath D:\MongoDB\data\db --config is MongoDB configuration path --dbpath is data store path for MongoDB |
Now open windows command prompt and go to MongoDB server bin directory (D:\MongoDB\mongodb-win32-x86_64-2008plus-2.6.0\bin) then run above command in command prompt.
The following screen shot to start MongoDB server
Connect and use MongoDB
Now we already started MongoDB server now we will connect to server.
Following command to connect to MongoDB server
Mongo |
Now open another command prompt and go to bin directory of MongoDB server then enter above command now you can see MongoDB shell to interact with server
The following is screen to show connecting to MongoDB server
Note:
When we connect to server it will show connecting to test database.
Interacting with MongoDB server
The following are important commands to interact with MongoDB
show dbs : List all databases. use db_name : Switches to the database. show collections : List all tables in the current selected database. |
The following screen to show databases in MongoDB server
Switch to other database
Note:
When we switch to database if database already existed it will switch to that database if not then it will be create new database with that name.
So MongoDB wont throws error even database not existed simply it will create same thing we can apply for table.
Display All Tables for Selected Database
Create Table and Store Data
In MongoDB when we store data, if the table not existed then it will create and insert data.
If available then it will store in existed table that we specifies.
The following is command insert data in table
db.users.insert({username:"meera",gender:"male"}) or db.users.save({username:"meera",gender:"male"}) |
We will use key value pair to represent Colum and its value
In the above users is table and username and gender its columns and values meera , male respectively.
The following screen to insert data in user table
Update Record db.users.update ({username:"meeraprince",gender:"male"}) List all records from table “users” db.users.find() Find records where username is “meera” db.users.find({username:"meera"}) Find records where username’s length is less than or equal to 4 db.users.find({$where:"this.username.length<=4"}) Delete Record db.users.remove({username:"meera"}) |
Reference Links
Author
0 comments:
Post a Comment