Showing posts with label types of nosql. Show all posts
Showing posts with label types of nosql. Show all posts

Saturday, September 20, 2014

MongoDB CRUD Operations

               
       MongoDB CRUD stands for Create, Read, Update, Delete. MongoDB provides rich semantic to communicate with the database. MongoDB CRUD operations are much similar to SQL create,select,update and delete operations. If you already work with SQL you will find it very easy to understand and if your not then still you will find it very easy! OK, before moving forward to CRUD operation we have to understand how MongoDB stores data. As we know MongoDB is document oriented database it stores the data in the form of documents.
      So no rows and columns. It uses JSON(JavaScript Object Notation) like format to store data. Formally, MongoDB documents are BSON documents. BSON is a binary representation of JSON with additional type information. following image is example of MongoDB document.

     Above document shows fields and values which is much similar to the relational database table but MongoBD uses dynamic schema means you not need to specify datatype, fields before entering data into database such as relational database management system. Following image is example of tabular database.
 In MongoDB there are collections instead of table. In MongoDB database contains number of collections MongoDB stores all the documents in collection. Collection is a group of related documents that have a set of shared common indexes.

Now lets see MongoDB CRUD operations, Mainly include MongoDB read operation and Write operation. It contains some important points such as Cursor, query optimization and distributed queries but we will perform simple operations first for better understanding.
    We will perform the tasks in following order :
  1. Create 
  2. Insert 
  3. Read 
  4. Update 
  5. remove
       Before start performing this operations we have to setup environment means making connection between "mongod" server and "mongo" client. If you don't know how to startup mongo shell then read my previous blog "Downloading and installing MongoDB on windows" 

1. Create:
   lets start from creating database in MongoDB. Now what you have to do is just enter following command on mongo shell,
   Syntax:
          use databasename
  Example:
        use blogdata 
   Database is not created unless you not inserting documents in it.


2. Insert:
     To write into database following syntax is used,
 Syntax:
             db.collectionname.insert({ document to be inserted});

      You can use any other folder name instead of "db" if you want.You don't have to create collection separately. It will automatically get created.
 example:
            
                             

            
        To see the document that you have inserted run this command "db.collectionname.find()", "collectionname" should be the same which you have used earlier to insert document.


  3. Read:
           To read data from the database following command is used,
   Syntax:
                 db.collectionname.find();
    By using this command documents will get displayed, you can be more specific by writing some criteria in find(), by default it will display all documents that contains in collection. But if you want to get specific document then Mongo provides way to write more optimized query you can use :

  • Limit the cursor means you can limit the number of result you get which matches criteria.
  • Can add projections means you can specify which fields you want in result.
  • Can add query criteria 

    Example: 
                      
     This query returns documents in which age is greater than 18. It will display only name and address field because it is specified as "1" if field is "0" then mongo does not display that fields in result. Look at following SQL query which gives same result as mongo query for more better understanding.

         
    4. Update: 
           Update operation is used to modify the documents,
       Syntax:
                  db.collectionname.update({ criteria})
       Example:
            Following query will update the document field "status" as "A" where field "age" has value greater than "18".

     Following SQL query gives the same result as mongo.

     
   5. Remove: 
        Remove operation is used  to delete documents from database,
     Syntax:
                      db.collectionname.remove({criteria})
     Example:
         Following query will remove the documents where "status" field is "D".


   See the SQL query for same operation, 

Friday, September 5, 2014

Introduction to MongoDB

     MongoDB is new generation document oriented NoSQL database. It is open source product and developed and supported by company 10gen (Now Mongo Inc.). MongoDB is available under GNU Affero General Public License and Apache License. 10gen created MongoDB in 2007 and its first product release was MongoDB 1.4 in 2009.

       Its definition given by 10gen:- “MongoDB is a scalable, open source, high performance, document oriented database.” 
            MongoDB is most popular document oriented NoSQL databases now days. To know more about NoSQL database and its categories read my previous blogs.
MongoDB is adopted by many mejor companies including eBay, The New York Times, CERN, SAP, MetLife, MTV, Forbes, Sourceforge, Codecademy, Shutterfly, Foursquare and more.

 MongoDB provides following features:-
  • Ad hoc queries:- It supports search by field and regular expression search.
  • Indexing:- Any field in mongodb can be indexed.
  • Replication:- Supports master-slave replication. A master can perform           read-write and a slave copies data from the master and can only be used          for read or backup.
  •  Duplication of data:-Data duplication to keep the system up and running     in case of hardware failure.
  •  Scalability:- New machines can be added to run a database.
  •  Automatic Load Balancing:- Data is split into range and shared across        multiple nodes.
  • File storage:- Could be used as a file system taking advantage of load balancing and data replication.
  •  Aggregation:-MapReduce is available. Offers similar functionality as SQL GROUP by clause. 
  •  JavaScript execution:-Can use JavaScript functions.
  • Special support for location:- Understands longitude and latitude.
   

Monday, August 25, 2014

Types of NoSQL databases

Hello everyone, in my last blog “NoSQL” I have just listed different categories of NoSQL databases. Now let’s see it in details one by one.
  1.  Key value store
  2. Document store
  3. Column Family store
  4. Graph based


1.     Key value store:
·        This type of model is the most simplest and easiest to implement. The main idea here is use of hash table in which there exist a unique key and a value associated with it.
·        There is no complexity around the key value store data model as it can be implemented very fast and easily.
·        The key can be synthetic or auto generated while the value can be string, JSON, BSON etc.
·         It is schema free and your value is stored as a key eg. In one column you will be having key “Name” and value would be “Kaveri” and in the second column, it’s not necessary mean that you must have the value of name again; it could be different kind of data in the same column in different row.
·        Following table shows the example of key value store data model :
key
Value
“India”
{“09, Sai Anand App, Gangapur Road, Nasik 422013 ,India”}
“US”
{“3975 Fair Ridge Drive. Suite 200 South, Fairfax, VA 22033″}
“CA”
{“47112,warm springs blvd #103 Fremont, CA, United States 94539”}

·        This key/value type database allow clients to read and write values using a key as follows:
§  Get(key): returns the value associated with the provided key.
§  Put(key, value): associates the value with the key.
§  Multi-get(key1, key2, .., keyN): returns the list of values associated with the list of keys.
§  Delete(key): removes the entry for the key from the data store.
·        Example of key value store databases are: Memcached, Coherence,  Redis ,Riak and Amazon’s DynamoDB are the most popular key-value store NoSQL databases.
2.     Document store:
·        These kinds of NoSQL databases are very interesting because instead of rows and columns data is stored in documents, this semi structured data is simply stored in JSON, BSON.
·        Document database were inspired by Lotus and are similar to key value store. The model is basically documents that are collection of other key value collections.
·        Document databases are next level of key value. It supports querying more efficiently.
·        Document database would look like,


·        Example of  document databases are: Mango,CouchDB,Cloudant.
3. Column family store: 
·        Column family types were created to store and process very large amount of data distributed over many machines.
·        This type of NoSQL databases are not schema free. They are kinds of semi structured database which means you need to specify a group of column in these databases and they are called column families.
·        There are still keys but they point to multiple column arranged by column family.
·        A column family database can have different column on each row so is not relational and doesn’t qualifies in an RDBMS as a table
·        The implementation of column family much similar to the Google’s BigTable .
·        Colum family work better with complex dataset.
·        Example of column family databases are BigTable,Hbase,Accumulo.
4. Graph databases:
·        Instead of table of rows and columns and fixed structure of SQL, a flexible graph model is used which can gain scale across the multiple machines.
·        Graph structures are used with edges, nodes and properties which provides index-free adjacency. Data can be easily transformed from one model to the other using a Graph Base NoSQL database.
·        Graph database takes the document database to the extreme by introducing the concept of type of relationship between document and nodes. For example relationship present between the people on social network such as Facebook.
·        It uses sophisticated shortest path algorithm to make data querying more efficient.
·        Although it is slower than its other NoSQL counter paths a graph database can have the most complex structure and still traverse billions of nodes and relationship with light speed.


·        Example of graph databases are: Neo4j,Sones, InfiniteGraph, AllegroGraph, OrientDB, InfoGrid