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:
- Key-Value stores:
DynamoDB
, MemCacheDB
, Redis - Graph Stores:
Allegro
, InfiniteGraph
- Column Stores:
Accumulo
, Cassandra
- 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 string
s, and values can be stored as string
s, 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.
MongoDB
- Download the appropriate version of Mongo DB from this link: MongoDB Downloads
- Install the msi file which you have downloaded.
- 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.
- Open Command prompt in administrator mode and browse to the MongoDB directory as:
> cd C:\Program Files\MongoDB\Server\3.2\bin
- 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
- The above will display and output as:
- Open another command prompt and again browse to MongoDB directory as:
> cd C:\Program Files\MongoDB\Server\3.2\bin
- Type:
> Mongo
This will provide a test connection with the version of MongoDB as shown below:
- 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
.
- 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.
- Now we will fetch (select) the records as below:
> db.customerdetails.find()
This will give an output as below:
- 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.
- Three database commands to know:
The output is displayed as below:
show dbs
– List all databases use db_name
– Switches to db_name show collections
– List all tables in the current selected database
- 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:
- 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:
- 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.
- To delete a record, you can use “
remove
” syntax as below:
[__strong__]db.customerdetails.remove({"_id": "2"})
The output would be as:
- 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.