You are on page 1of 24

INTRODUCTION

• MongoDB is a database which came into light around the mid-2000s.

• MongoDB is a NoSQL database. It is an open-source, cross-platform, document-oriented database

• It is used for high volume data storage.

• Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections
and documents.

• Documents consist of key-value pairs which are the basic unit of data in MongoDB. Collections contain sets
of documents and function which is the equivalent of relational database tables.

• MongoDB performs operations such as insert documents, update documents, delete documents, query
documents, projection, sort() and limit() methods, create a collection, drop collection, etc.
History of MongoDB

• MongoDB was developed by a NewYork based organization named 10gen which is now
known as MongoDB Inc.

• It was initially developed as a PAAS (Platform as a Service). Later in 2009, it is


introduced in the market as an open source database server that was maintained and
supported by MongoDB Inc.

• The first ready production of MongoDB has been considered from version 1.4 which was
released in March 2010.

• MongoDB2.4.9 was the latest and stable version which was released on January 10,
2014.
Purpose of MongoDB
All the modern applications require big data, fast features development, flexible deployment, and the older database
systems not competent enough, so the MongoDB was needed.

The primary purpose of building MongoDB is:


•Scalability
•Performance
•High Availability
•Scaling from single server deployments to large, complex
multi-site architectures.
•Key points of MongoDB
•Develop Faster
•Deploy Easier
•Scale Bigger
The Create Collection
• MongoDB db.createCollection(name, options) is used to create collection.
SYNTAX:

Name: is a string type, specifies the name of the collection to be created.


Options: is a document type, specifies the memory size and indexing of the collection. It is an optional parameter.
Example

Let's take an example to create collection.


In this example, we are going to create a collection name SSSIT.
> db.createCollection("SSSIT")
To check the created collection, use the command "show collections".
> show collections
O/P: SSSIT
How does MongoDB create collection automatically

MongoDB creates collections automatically when you insert some documents. For
example: Insert a document named seomount into a collection named SSSIT. The
operation will create the collection if the collection does not currently exist.
> db.SSSIT.insert({"name" : "seomount"})
> show collections
SSSIT
Drop Collection
The drop() Method
MongoDB's db.collection.drop() is used to drop a collection from the database.
SYNTAX

First, check the available collections into your database mydb.

drop() method will return true, if the selected collection is


dropped successfully, otherwise it will return false
Insert Document
• The insert() Method
• To insert data into MongoDB collection, you need to use MongoDB's insert()
The basic syntax of insert() command is as follows −

Eg:
Example
After the successful insertion of the
document, the operation will return a
WriteResult object with its status.
Check the inserted documents

>db.javatpoint.find()
MongoDB insert multiple documents

• If you want to insert multiple documents in a collection, you have to


pass an array of documents to the db.collection.insert() method.

• Create an array of documents


Cont..
> db.javatpoint.insert( Allcourses );

Note: Here the nInserted field specifies the number of documents inserted. In the
case of any error during the operation, the BulkWriteResult will specify that error.
Update Documents

• In MongoDB, update() method is used to update or modify the


existing documents of a collection.
CRUD Operation

C- Create Operation
R- Read Operation
U- Update Operation
D- Delete Operation
Create Operations

• Create or insert operations add new documents to a collection. If the


collection does not currently exist, insert operations will create the
collection.
• MongoDB provides the following methods to insert documents into a
collection:

Eg:
Read Operation

• Read operations retrieve documents from a collection; i.e. query a


collection for documents.
• MongoDB provides the following methods to read documents from a
collection:
Eg:
Update Operation

• Update operations modify existing documents in a collection.


• MongoDB provides the following methods to update documents of a
collection:

Eg:
Delete Operation

• Delete operations remove documents from a collection.


• MongoDB provides the following methods to delete documents of a
collection:

Eg:
Advantages of MongoDB over RDBMS

• Schema less − MongoDB is a document database in which one collection holds


different documents. Number of fields, content and size of the document can differ
from one document to another.
• Structure of a single object is clear.
• Deep query-ability. MongoDB supports dynamic queries on documents using a
document-based query language that's nearly as powerful as SQL.
• Ease of scale-out − MongoDB is easy to scale.
• Uses internal memory for storing the (windowed) working set, enabling faster
access of data.
Thank You

You might also like