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 languagesWhat 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 commanddb.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
Post a Comment