Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
95 views8 pages

Mongo DB

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

MongoDB is a cross-platform, document-oriented database that provides,

high performance, high availability, and easy scalability. MongoDB works on


concept of collection and document.

1. Each database contains collections which in turn contains


documents. Each document can be different with a varying number
of fields. The size and content of each document can be different
from each other.
2. The document structure is more in line with how developers
construct their classes and objects in their respective programming
languages. Developers will often say that their classes are not rows
and columns but have a clear structure with key-value pairs.
3. As seen in the introduction with NoSQL databases, the rows (or
documents as called in MongoDB) doesn't need to have a schema
defined beforehand. Instead, the fields can be created on the fly.
4. The data model available within MongoDB allows you to represent
hierarchical relationships, to store arrays, and other more complex
structures more easily.

1. Scalability – The MongoDB environments are very scalable. Companies


across the world have defined clusters with some of them running 100+
nodes with around millions of documents within the database

Database
Database is a physical container for collections. Each database gets its own
set of files on the file system. A single MongoDB server typically has multiple
databases.

Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS
table. A collection exists within a single database. Collections do not enforce
a schema. Documents within a collection can have different fields. Typically,
all documents in a collection are of similar or related purpose.

Document

Sensitivity: Internal & Restricted


A document is a set of key-value pairs. Documents have dynamic schema.
Dynamic schema means that documents in the same collection do not need
to have the same set of fields or structure, and common fields in a collection's
documents may hold different types of data.

Key Components of MongoDB Architecture


Below are a few of the common terms used in MongoDB

1. _id – This is a field required in every MongoDB document. The _id field
represents a unique value in the MongoDB document. The _id field is like
the document's primary key. If you create a new document without an _id
field, MongoDB will automatically create the field.

2. Collection – This is a grouping of MongoDB documents. A collection is the


equivalent of a table which is created in any other RDMS such as Oracle or
MS SQL. A collection exists within a single database. As seen from the
introduction collections don't enforce any sort of structure.
3. Cursor – This is a pointer to the result set of a query. Clients can iterate
through a cursor to retrieve results.
4. Database – This is a container for collections like in RDMS wherein it is a
container for tables. Each database gets its own set of files on the file
system. A MongoDB server can store multiple databases.
5. Document - A record in a MongoDB collection is basically called a
document. The document, in turn, will consist of field name and values.
6. Field - A name-value pair in a document. A document has zero or more
fields. Fields are analogous to columns in relational databases.

The following diagram shows an example of Fields with Key value pairs. So
in the example below CustomerID and 11 is one of the key value pair's
defined in the document.

Sensitivity: Internal & Restricted


7. JSON – This is known as JavaScript Object Notation. This is a human-
readable, plain text format for expressing structured data. JSON is
currently supported in many programming languages.

Just a quick note on the key difference between the _id field and a normal
collection field. The _id field is used to uniquely identify the documents in a
collection and is automatically added by MongoDB when the collection is created.

MongoDB stores data records as BSON documents. BSON is a binary representation


of JSON documents, though it contains more data types than JSON.

A record in MongoDB is a document, which is a data structure composed of field and


value pairs. MongoDB documents are similar to JSON objects. The values of fields may

include other documents, arrays, and arrays of documents.

The advantages of using documents are:

 Documents (i.e. objects) correspond to native data types in many programming


languages.
 Embedded documents and arrays reduce need for expensive joins.
 Dynamic schema supports fluent polymorphism.

The following table shows the relationship of RDBMS terminology with


MongoDB.

Sensitivity: Internal & Restricted


RDBMS MongoDB

Database Database

Table Collection

Tuple/Row Document

Column Field

Table Join Embedded Documents

Primary Key Primary Key (Default key _id provided


by mongodb itself)

Database Server and Client

Mysqld/Oracle Mongod

mysql/sqlplus Mongo

Sample Document
Following example shows the document structure of a blog site, which is
simply a comma separated key value pair.

_id: ObjectId(7df78ad8902c)

title: 'MongoDB Overview',

description: 'MongoDB is no sql database',

by: 'tutorials point',

url: 'http://www.tutorialspoint.com',

Sensitivity: Internal & Restricted


tags: ['mongodb', 'database', 'NoSQL'],

likes: 100,

comments: [

user:'user1',

message: 'My first comment',

dateCreated: new Date(2011,1,20,2,15),

like: 0

},

user:'user2',

message: 'My second comments',

dateCreated: new Date(2011,1,25,7,45),

like: 5

_id is a 12 bytes hexadecimal number which assures the uniqueness of every


document. You can provide _id while inserting the document. If you don’t
provide then MongoDB provides a unique id for every document. These 12
bytes first 4 bytes for the current timestamp, next 3 bytes for machine id,
next 2 bytes for process id of MongoDB server and remaining 3 bytes are
simple incremental VALUE.

MongoDB - Advantages
Any relational database has a typical schema design that shows number of
tables and the relationship between these tables. While in MongoDB, there is
no concept of relationship.

Advantages of MongoDB over RDBMS

Sensitivity: Internal & Restricted


 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.

 No complex joins.

 Deep query-ability. MongoDB supports dynamic queries on documents using a


document-based query language that's nearly as powerful as SQL.

 Tuning.

 Ease of scale-out − MongoDB is easy to scale.

 Conversion/mapping of application objects to database objects not needed.

 Uses internal memory for storing the (windowed) working set, enabling faster
access of data.

Why Use MongoDB?


Below are the few of the reasons as to why one should start using MongoDB

1. Document-oriented – Since MongoDB is a NoSQL type database, instead of


having data in a relational type format, it stores the data in documents.
This makes MongoDB very flexible and adaptable to real business world
situation and requirements.
2. Ad hoc queries - MongoDB supports searching by field, range queries, and
regular expression searches. Queries can be made to return specific fields
within documents.
3. Indexing - Indexes can be created to improve the performance of searches
within MongoDB. Any field in a MongoDB document can be indexed.
4. Replication - MongoDB can provide high availability with replica sets. A
replica set consists of two or more mongo DB instances. Each replica set
member may act in the role of the primary or secondary replica at any
time. The primary replica is the main server which interacts with the client
and performs all the read/write operations. The Secondary replicas
maintain a copy of the data of the primary using built-in replication. When
a primary replica fails, the replica set automatically switches over to the
secondary and then it becomes the primary server.
5. Load balancing - MongoDB uses the concept of sharding to scale
horizontally by splitting data across multiple MongoDB instances.

Sensitivity: Internal & Restricted


MongoDB can run over multiple servers, balancing the load and/or
duplicating data to keep the system up and running in case of hardware
failure.

Where to Use MongoDB?


 Big Data

 Content Management and Delivery

 Mobile and Social Infrastructure

 User Data Management

 Data Hub

Key Features

High Performance

MongoDB provides high performance data persistence. In particular,

 Support for embedded data models reduces I/O activity on database system.
 Indexes support faster queries and can include keys from embedded documents and
arrays.

Rich Query Language

MongoDB supports a rich query language to support read and write operations
(CRUD) as well as:

 Data Aggregation
 Text Search and Geospatial Queries.

High Availability

MongoDB’s replication facility, called replica set, provides:

 automatic failover and


 data redundancy.

Sensitivity: Internal & Restricted


A replica set is a group of MongoDB servers that maintain the same data set, providing
redundancy and increasing data availability.

Horizontal Scalability

MongoDB provides horizontal scalability as part of its core functionality:

 Sharding distributes data across a cluster of machines.


 Starting in 3.4, MongoDB supports creating zones of data based on the shard key. In a
balanced cluster, MongoDB directs reads and writes covered by a zone only to those
shards inside the zone. See the Zonesmanual page for more information.

Support for Multiple Storage Engines

MongoDB supports multiple storage engines:

 WiredTiger Storage Engine (including support for Encryption at Rest)


 In-Memory Storage Engine
 MMAPv1 Storage Engine (Deprecated in MongoDB 4.0)

In addition, MongoDB provides pluggable storage engine API that allows third parties to
develop storage engines for MongoDB.

Sensitivity: Internal & Restricted

You might also like