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
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 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 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 are used to store information about networks, such as social connections. Graph stores include Neo4J and HyperGraphDB.
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 such as Cassandra and HBase are optimized for queries over large datasets, and store columns of data together, instead of rows.
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
We have installation files and binary distribution, we can use any thing.
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.config file 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
##all output go here
##log read and write operations
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
MongoDB Server Bin directory Path
MongoDB data store path
MongoDB configuration file path
MongoDB log file path
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
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
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
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
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
List all records from table “users”
Find records where username is “meera”
Find records where username’s length is less than or equal to 4