Skip to main content

MongoDB - NoSQL Database

 

MongoDB - NoSQL Database

MongoDB is a NoSQL database written in C++ language. Some of its drivers use the C programming language as the base. MongoDB is a document oriented database where it stores data in collections instead of tables. The best part of MongoDB is that the drivers are available for almost all the popular programming languages

What is Document based storage?

A Document is nothing but a data structure with name-value pairs like in JSON. Also, excessive JOINS can be avoided by saving data in form of Arrays and Documents(Embedded) inside a Document.

For example : Student object has attributes name, rollno and subjects, where subjects is a List.
Document for Student in MongoDB will be like :

{
 name : "Ashish Singh",
 rollno : 7011,
 subjects : ["C Language", "C++", "Core Java"]
}
 

History of MongoDB

MongoDB was developed by Eliot Horowitz and Dwight Merriman in the year 2007, when they experienced some scalability issues with the relational database while developing enterprise web applications at their company DoubleClick. According to Dwight Merriman, one of the developers of MongoDB, this name of the database was derived from the word humongous to support the idea of processing large amount of data.

MongoDB consists of a set of databases. Each database again consists of Collections. Data in MongoDB is stored in collections. The below figure depicts the typical database structure in MongoDB.



Database : Database in MongoDB is nothing but a container for collections. Below syntax is used to create Database. 

Syntax :-use DATABASE_NAME 

 This will create a new database with the name database_name if there is no database already present with the same name. If a database already exists with the mentioned name, then it just connects to that database.
 

1. Show all the Database :-

Syntax :- show dbs

2. Check current connected database :-

Syntax :- db 

Collections :-

Collection is nothing but a set of MongoDB documents. These documents are equivalent to the row of data in tables in RDBMS.Collections are a way of storing related data. Being schemaless, any type of Document can be saved in a collection. Document's can have a maximum size of 4MB.

Documents:
Document in MongoDB is nothing but the set of key-value pairs. Snce MongoDB is considered as a schema-less database, each collection can hold different type of objects. Every object in a collection is known as Document, which is represented in a JSON like (JavaScript Object Notation) structure(nothing but a list of key-value pair). Data is stored and queried in BSON, its binary representation of JSON-like data.

Example of Array as value

{ _id : 112233,
  name : "Viraj",
  education : [
           {
              year : 2029,
              course : "BTECH",
              college : "IIT, Delhi"
            },
           {
             year : 2031,
             course : "MS",
             college : "Harvard College"
           }
  ]
}


MongoDB structures/stores the data in the form of BSON (Binary representation of JSON) which is a self-explaining and human readable data format and the way it provides the scaling feature to the application to be developed

Creating a Collection

In MongoDB a collection is automatically created when it is referenced in any command. For example, if you run an insert command :

db.student.insert({
  name: "Viraj"
})
 
Above command will create a collection named student if it doesn't exist already in the database. But we can explicitly create a collection using the createCollection() command. The syntax of createCollection method is :

db.createCollection(name, options)
 

CRUD Operations in MongoDB

Inserting a document into a collection (Create)

The command db.collection.insert() will perform an insert operation into a collection of a document.
Let us insert a document to a student collection. You must be connected to a database for doing any insert:-

db.student.insert(
{
 regNo: "3014",
 name: "Test Student",
 course: {
  courseName: "MCA",
  duration: "3 Years"
 },
 address: {
  city: "Bangalore",
  state: "KA",
  country: "India"
 }
})
 

Querying a document from a collection (Read) 

To retrieve (Select) the inserted document, run the below command. The find() command will retrieve all the documents of the given collection.

Syntax :-db.collection_name.find() 

 ex :-db.student.find()

ex :-db.student.find().pretty()

Updating a document in a collection (Update)

In order to update specific field values of a collection in MongoDB, run the below query.
db.collection_name.update()

 ex :-
 
db.student.update({
 "regNo": "3014" 
},
{
$set:
{"name":"Viraj"}
})

Removing an entry from the collection (Delete)

Let us now look into the deleting an entry from a collection. In order to delete an entry from a collection, run the command as shown below :
db.collection_name.remove({"fieldname":"value"})
 
eg.db.student.remove({"regNo":"3014"}) 
 
 

Comments

Popular posts from this blog

OOPs Concept For Automation

The java.util package contains all the classes and interfaces of Collection Framework.   Two Main root interfaces : Collection & Map  Parent Interface of Collection is Interable Iterable Interface  : -The Iterable interface is the root interface for all the collection classes and all the collection class implement the Iterable interface. Iterator< T > iterator(); Iterator interface provides the facility of iterating the elements in a forward direction only. // Returns true if the iteration has more elements public boolean hasNext(); // Returns the next element in the iteration // It throws NoSuchElementException if no more  // element present public Object next(); // Remove the next element in the iteration // This method can be called only once per call // to next() public void remove(); interface Collection< E > extends Iterable< E > interface Map< K , V > Collection : Root int...