Click here to Skip to main content
15,868,005 members
Articles / Database Development / NoSQL

NoSQL: MongoDB for Beginners

Rate me:
Please Sign up or sign in to vote.
4.71/5 (15 votes)
21 Feb 2016CPOL4 min read 15.7K   35   1
NoSQL: MongoDB for beginners

NoSQL: MongoDB for Noobs

What is NoSQL?

NoSQL, i.e., not only SQL is a non-relational based database, which 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 this link: MongoDB Downloads
  2. Install the msi file which you have downloaded.
  3. 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.

  4. Open Command prompt in administrator mode and browse to the MongoDB directory as:
    C#
    > cd C:\Program Files\MongoDB\Server\3.2\bin 
  5. 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
  6. The above will display and output as:

    1

  7. Open another command prompt and again browse to MongoDB directory as:
    C#
    >  cd C:\Program Files\MongoDB\Server\3.2\bin 
  8. Type:
    C#
    >  Mongo

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

    2

  9. Now we will create a database. In MongoDB, both database and table are created automatically when the first time data is inserted. Use database-name, to switch to your database (even this is not created yet).
    C#
    >  use customer

    This will create a database called Customer.

  10. Create a table inside Customer:
    C#
    >  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:

    C#
    >  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.

  11. Now we will fetch (select) the records as below:
    C#
    >  db.customerdetails.find()

    This will give an output as below:

    3

  12. 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:
    C#
    > db.customerdetails.insert({_id:"2″,Name:"XYZ"})

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

    4

  13. Three database commands to know:

    The output is displayed as below:

    5

    • show dbs – List all databases
    • use db_name – Switches to db_name
    • show collections – List all tables in the current selected database
  14. Now we will try to update an inserted record. Let us suppose we will update Name : “XYZ” to “Swami” where id : “2”. For this, we will write as below:
    C#
    >  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:

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

    The output is as below:

    6

  15. 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

  16. You can also refine your fetch results as below:
    C#
    >  db.customerdetails.find({"id": "1"})
    C#
    >  db.customerdetails.find({$where:"this.Name.length > 3″})

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

    8

  17. To delete a record, you can use “remove” syntax as below:
    C#
    db.customerdetails.remove({"_id": "2"})

    The output would be as:

    9

  18. 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.

Image 11

License

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


Written By
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/

Comments and Discussions

 
GeneralMy vote of 4 Pin
Donsw27-Feb-16 15:12
Donsw27-Feb-16 15: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.