Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

NoSQL: MongoDB for Beginners

0.00/5 (No votes)
21 Feb 2016 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:
    > 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:
    >  cd C:\Program Files\MongoDB\Server\3.2\bin 
  8. Type:
    >  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).
    >  use customer

    This will create a database called Customer.

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

  11. Now we will fetch (select) the records as below:
    >  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:
    > 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:
    >  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:

    [__strong__] 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:
    >  db.customerdetails.find({"id": "1"})
    >  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:
    [__strong__]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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here