Click here to Skip to main content
13,254,154 members (61,128 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.9K views
32 bookmarked
Posted 21 Feb 2016

NoSQL: MongoDB for beginners

, 21 Feb 2016
Rate this:
Please Sign up or sign in to vote.
NoSQL: MongoDB for Noob’s What is NoSQL? NoSQL i.e. Not Only SQL is a Non-relational based database , provides a mechanism for storage and retrieval of data that is modelled in means other than the tabular relations used in relational databases.

NoSQL: MongoDB for Noob’s

What is NoSQL?

NoSQL i.e. Not Only SQL is a Non-relational based database , provides a mechanism for storage and retrieval of data that is modelled in means other than the tabular relations used in relational databases.

It is divided into 4 types:

  1. Key-Value stores : DynamoDB, MemCacheDB, Redis
  2. Graph Stores : Allegro, InfiniteGraph
  3. Column Stores : Accumulo,Cassandra
  4. Document Stores : MongoDB, Azure DocumentDB, Lotus Notes, CouchDB

MongoDB is a type of Document Store No-SQL database which stores records as “documents” where a document can generally be thought of as a grouping of key-value pairs (it has nothing to do with storing actual documents such as a Word document).  Keys are always strings, and values can be stored as strings, numeric, Booleans, arrays, and other nested key-value pairs.  Values can be nested to arbitrary depths.  In a document database, each document carries its own schema — unlike an RDBMS, in which every row in a given table must have the same columns.

0

MongoDB:

  1. Download the appropriate version of Mongo DB from the below link :

MongoDB Downloads

  1. Install the msi file which you have downloaded.
  1. After install, you will be able to find your MongoDB (ideally) at : “C:\Program Files\MongoDB”

         Now we will look at how to setup MongoDB environment

  1. Open Command prompt in administrator mode and browse to the MongoDB directory as :

           > cd C:\Program Files\MongoDB\Server\3.2\bin

  1. MongoDB requires a data directory to store all data. Its default data directory path is “\data\db”. You can specify an alternate path as below. Please create the directory initially before running the below command:

          > mongod.exe –dbpath d:\MongoDB\data

  1. The above will display and output as

1

  1. Open another command prompt and again browse to MongoDB directory as :

          > cd C:\Program Files\MongoDB\Server\3.2\bin

  1. Type

         > Mongo

           This will provide a test connection with the version of MongoDB as shown below

2

 

  1. Now we will create a database. In MongoDB, both database and table are created automatically when the first time data is inserted. Usesuse database-name, to switch to your database (even this is not created yet).

           > use customer

           This will create a database called Customer

  1. Create a table inside Customer.

           > db.customerdetails.insert({id:”1″,Name:”Aditya”,Location:”India”})

           The above statement will create a table called “customerdetails” and insert a record              with id : 1 , Name : Aditya and Location : India

We will insert another record :

          > db.customerdetails.insert({id:”2″,Name:”XYZ”})

           As you can find above, the column names are not fixed and can keep changing. As     well as the data and number of columns. This is the advantage of Non-relational database.

  1. Now we will fetch (select) the records as below :

           > db.customerdetails.find()

          This will give an output as below:

3

  1. The “_id” is the unique field used to distinguish each row in MongoDB. You can also give a custom “_id” by using “_id” while inserting as below

          > db.customerdetails.insert({_id:”2″,Name:”XYZ”})

         This will display output as below once we fetch the records,

4

  1. Three database commands to know :

show dbs – List all databases.

use db_name – Switches to db_name.

show collections – List all tables in the current selected database.

The output is displayed as below :

5

  1. Now we will try to update an inserted record. Let us suppise we will update Name : “XYZ” to “Swami” where id : “2”. For this we will write as below

           > db.customerdetails.update({“id”:”2″},{$set:{“Name”:”Swami”}})

           The above statement will fetch a record with  id = 2 and update Name : “Swami”. The syntax being:

           db.tablename.update({ criteria },{$set: {new value}})

          The output as below

6

  1. Now what will happen if we have 2 records with same id i.e id=1 . It will update the record which it finds first as below.

7

  1. You can also refine your fetch results as below

          > db.customerdetails.find({“id”: “1”})

         > db.customerdetails.find({$where:”this.Name.length > 3″})

        Note : in above statement, if “length” is put as “Length” the result would not be yielded. All keywords are case sensitive.

8

  1. To delete a record, you can use “remove” syntax as below,

           > db.customerdetails.remove({“_id”: “2”})

           The output would be as :

9

  1. To view the database and data in the MongoDB on the browser you need to enable rest option while turning on the database as (Instead of Step 5),

           > mongod.exe –dbpath d:\MongoDB\data –rest

           Though this has some security implications, you will be able to view and use this data easily.

Then you would be able to view using URL :

http://localhost:28017/

This is ideally Administrators view. (1000 more than the port which is open i.e 27017)

You can view the data using the URL

http://127.0.0.1:28017/customer/customerdetails/

The syntax being :

http://127.0.0.1:28017/databaseName/collectionName/

In our further tutorials we will look at how to access this Non-relational MongoDB.

 

 

 


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

adityaswami89
Software Developer (Senior)
India India
Passionate about Microsoft Technologies like WPF, Windows Azure, ASP.NET, Win Phone and also on Cross platform Mobile Apps, Mongo DB's, IOT & WOT. I love learning and working with (or) Creating Design Patterns . Writer | Technology Evangelist | Technology Lover | Microsoft Developer Guidance Advisory Council Member | Cisco Champion | Speaker |

Blog : http://adityaswami89.wordpress.com/

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Donsw27-Feb-16 16:12
memberDonsw27-Feb-16 16:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 22 Feb 2016
Article Copyright 2016 by adityaswami89
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid