Tuesday, September 20, 2016

MongoDB -- Getting Started

MongoDB
In object-oriented development, we are encouraged to follow code development through logical models approach; so that developers can more readily conceptualise it in his mind.

What if you could store the programmatic models almost exactly like you model them? 


What if you could store them as they are instead on in series of rows in the tables?

By learning about MongoDB, you will be able to do just that (aforementioned two questions).


This tutorial is basic of MongoDB, such as installation, creating, updating and deleting databases and records. Perform search and complex searches.


So without much ado --- let's get started!


Installing MongoDB
Go to the MongoDB website and click on download link.





This will bring you to a page where you can get a build for your platform. 
The MongoDB will show the following messages and options in Windows x64 environment:

Step - 1

Step - 2






















Step - 3






















Step - 4






















Step - 5






















When you are done installing the MongoDB with the help of above mentioned steps and link. Please perform the followings:

1) Create "db" folder inside "data" folder at C drive.

2) Run Mongod.exe from the installation_folder\bin folder and let it running.

Mongod.exe will look like:















Open another terminal (DOS), go to the MongoDB installation folder location using CD command. Type mongo.exe, that will open up the Mongo shell. You'll see output similar to below:















OK, let's get started with the followings:

1) Creating a database

2) Inserting records
3) Updating records
4) Deleting records
5) Searching records


MongoD - Create Database
Use Database statement will create database. Please see below as given:


>use midb
switched to db midb


To check your currenly selected database use the command db.


>db
midb


If you want to check your databases list, then use the command show dbs.

>show dbs

local            0.078GB


Your created database (midb) is not present in the list. To display your database, you need to insert atleast one document into it.


>db.city.insert({name:'
Bhopal'});
>show dbs
local            0.078GB
midb           0.078GB


In MongoDB default database is test. if you didn't create any database then collections will be stored in test database.
MongoDB - Insert Records
>db.employee.insert({
    first_name: 'Mohammed',
    last_name: 'ishtiyaq',
    dob: '06/03/1976',
    gender: 'M',
    designation: 'dba',
    salary: '3000',
    active_ind: 'Active',
    created_on: '13/04/2015'
  });


db.employee.insert({
    first_name: 'Arzoo',
    last_name: 'Nizam',
    dob: '12/02/2004',
    gender: 'F',
    designation: 'software engineer',
    salary: '6000',
    active_ind: 'Active',
    created_on: '13/04/2015'
  });

db.employee.insert({
    first_name: 'Mahaveer',
    last_name: 'Jain',
    dob: '06/01/1978',
    gender: 'M',
    designation: 'dba',
    salary: '4000',
    active_ind: 'InActive',
    created_on: '13/04/2015'
  });


To confirm that the database and accompanying records have been created, use the below command:

db.employee.find();











If everything went well then you'll see the output as above given.

The above screen shows that all records are inserted in the database. One thing to nore here is "id" field. This is auto generated by Mongo, if you don't specify an ID. The ID makes each record a unique record.
MongoDB - Record Search
How do you find all females, DBAs, males and active employee? To find the specific record or information, you need to use selectors.

Selectors
Selecors are to MongoDB what where clauses are to SQL. As with where clauses, MongoDB selectors allow us to do the following:
·                     Specify criteria that MUST match.
·                     Specify criteria that CAN optionally match.
·                     Specify criterial that MUST exist.

Records That MUST Match
Find all employees who are female.

db.employee.find({gender : '
F'});

Running the above command will return the following output:









What if you want to search for male employees? Run the following command:


db.employee.find({gender : 'M'});

You'll get the following results:











Search with Multiple Criteria
You can search for male employees who are DBAs.

db.employee.find({gender : '
M', $or: [{designation: 'dba'}]} );


Running that will return the following results:











You can search the male employee, who are dba by designation and active employees.


db.employee.find({gender : 'M', $and: [{designation: 'dba'}], $and [{active_ind: 'Active'}]} );

The query will generate the following results:











 MongoDB - Sorting
What if you want to sort records, by first_name, designation or salary? Similar to SQL, MongoDB provides the sort command too.

Unlike SQL, specifying ascending and descending in MongoDB is different. You can do that as follows:
·                     Ascending: 1
·                     Descending: -1

Example:

db.employee.find({gender : 'M', $or: [{designation: 'dba'}]} ).sort({gender: -1});
MongoDB - Limiging Records
Just imagine if you have a big data set and you want to limit the results to just 5. MongoDB provides the limit command, similar to MS SQL Server Top command.

Example:

db.employee.find({gender : 'M', $or: [{designation: 'dba'}]} ).limit(1);
MongoDB - Update Records
MongoDB provides an option to update records. With the find method and SQL queries, you must specify the criteria for the record that has to update, then only the data in that record with be modified.

For example let's change Mahaveer's salary from 4000 to 6000.



db.employee.update({first_name: 'Mahaveer', last_name: 'Jain'}, {$set: {salary: '6000'}});






MongoDB - Deleting Records
If you want to delete a record, you have to pass in a set of selectors, as you do in the SQL, to determine the set of records to delete. If you don't do this, you'll delete all records, and if your database will have a single table then removing all records will also remove the database.

Delete example:



db.employee.remove({first_name: 'Mahaveer'});
-- To remove specific record
  
db.employee.remove();
-- To remove all records
Conclusion
In this beginning session, you'll be able to understand following:
·                     What MongoDB is?
·                     How to install MongoDB?
·                     How to create MongoDB database?
·                     How to insert, update and delete records in MongoDB?
·                     How to search, sort and limit records in MongoDB?


No comments:

Post a Comment