Mongo DB
Mongo DB
MongoDB
MongoDB is a document-oriented NoSQL database used for high-volume data storage. It
contains the data model, which allows you to represent hierarchical relationships. It uses JSON-
like documents with optional schema instead of using tables and rows in traditional relational
databases. Documents containing key-value pairs are the basic units of data in MongoDB.
History
Dwight Merriman, Eliot Horowitz, and Kevin Ryan created MongoDB in 2007. To provide a
solution for the problems of scalability and agility that they were facing at DoubleClick, they
decided to develop a database. That’s when MongoDB came into existence.
• MongoDB2.4.9 was the latest and stable version which was released on January 10,
2014.
• It became a publicly traded business on October 20, 2017, when it was listed on
NASDAQ as MDB with an IPO price of $24 per share.
Features of MongoDB
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.
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.
6. Easy to use
When the database grows, the challenge is to how to scale it. There are two common
ways:
• Scaling up – upgrade the current server to a bigger one with more resources (CPU,
RAM, etc). However, getting a bigger server means increasing more costs.
• Scaling out – purchase additional servers and add them to the cluster. This is cheaper
and more scalable than scaling up. The downside is that it takes more effort to manage
multiple servers than a big one.
MongoDB allows you to split data across many servers. It also automatically manages
the load balancing across the cluster, redistributing data, and routing updates to the
correct servers.
The following picture illustrates how MongoDB scale-out using sharding across
multiple servers:
8. Rich features
Like any database system, MongoDB allows you to insert, update, and delete, and select
data. In addition, it supports other features including:
• Indexing
• Aggregation
• File Storage
9. High performance
MongoDB was designed to maintain the high performance from both architecture and
feature perspectives.
RDBMS MongoDB
Not suitable for hierarchical data storage. Suitable for hierarchical data storage.
It is row-based. It is document-based.
It is slower in comparison with MongoDB. It is almost 100 times faster than RDBMS.
It is column-based. It is field-based.
MongoDB architecture is built on collections and documents. The basic unit of data in this
database consists of a set of key-value pairs. It allows documents to have different fields and
structures. This database uses a document storage format called BSON which is a binary style
of JSON documents.
• Database
• Collection
• Document
Database
It is also called the physical container for data. Every database has its own set of files existing
on the file system. In a single MongoDB server, there are multiple databases present.
Collection
The collection consists of various documents from different fields. All the collections reside
within one database. In collections no schemas are present.
Document
The set of key values are assigned to the document which is in turn associated with dynamic
schemas. The benefit of using these schemas is that a document may not have to possess the
same fields whereas they can have different data types.
DATA FORMATS
JSON
JSON stands for JavaScript Object Notation. JSON syntax is based on a subset of JavaScript
ECMA-262 3rd edition.
JSON is JavaScript Object Notation. It is a light-weight data interchange format, which can get
transferred from one system to the other easily. It is easy to read and write this format. It is also
not a complex task for machines to parse and generate. The text format is completely language
independent.
At high level, JSON will have two things- An object and an array. An object is a collection of
name/value pairs and an array is an ordered list of values.With the combination of two, you
can have a complete JSON structure. The maximum amount of documents that one can embed
in a document is 100. This is a very important factor while working with MongoDB.
So an object will start with curly braces and end with curly braces and then comes the key and
value. An array will start with the normal, non-curly brackets, then comes value and comma.
There are data types that can be supported. See the image below to understand it better.
"first_name": "Hasini",
"last_name": "Krishna",
"age": 22,
BSON
BSON is nothing but Binary JSON i.e Binary JavaScript Object Notation. Unlike JSON, it is
not in a readable format. It supports the embedding of documents and arrays within other
documents and arrays. Like JSON, it is easy for machines to parse and generate. When you
check the websites for information, you will see the difference in JSON and BSON and
understand how different the two are in terms of readability.
MongoDB stores documents in BSON, which is the binary encoded format of JSON. Basically,
the name BSON itself comes from Binary encoded JSON. The BSON data format provides
various types, used when we store the JavaScript objects in the binary form.
We can make remote procedure calls in MongoDB by using BSON. All the BSON data-types
are supported in MongoDB. Below are the enlisted MongoDB data types. Each MongoDB
datatypes corresponds a unique number which is used to identify them in $type method.
1) Integer
Integer is a data type that is used for storing a numerical value, i.e., integers as can save in other
programming languages. 32 bit or 64-bit integers are supported, which depends on the server.
Example:
{
"emp_name": "Shobha",
"emp_dept": "MCA",
"emp_id": 1,
}
emp_id is of the type integer since it stores a numeric number.
2) Double
Numeric numbers containing 8 bytes (64-bit IEEE 754 floating point) floating-point are stored
using the double data type. An example of a document with a double value in the field intern
score is shown below.
{
"emp_name": "Shobha",
"emp_dept": "MCA",
"emp_salary": 37500.56,
}
emp_salary is of the type double since it stores a numeric number.
PREPARED BY SHOBHA RANI BR 7
MONGODB
3) Boolean
Boolean (true or false) values are stored with the boolean data type. The field intern status is
of the type boolean in the example below because it stores the value true. Booleans take up less
space than integers or strings and avoid unwanted comparison side effects.
{
"emp_name": "Shobha",
"emp_status”: “true”,
"emp_salary": 37500.56,
}
emp_status is of the type Boolean.
4) String
This is the most commonly used MongoDB data types, BSON strings are UTF-8. Drivers for
each programming language convert from the string format of the language to UTF-8 while
serializing and de-serializing BSON.
This makes possible to easily store most international characters in BSON strings. The string
must be valid to be saved.
{
"emp_name": "Shobha",
"emp_dept”: “MCA”,
"emp_salary": 37500.56,
}
emp_name & emp_dept are of the type String.
5) Array
MongoDB data types stores array. A set of values are represented as an array. This data type
can store multiples of values and data types.
{
"emp_name": "Shobha",
"emp_skills”: [“React”,”Python”,”R”]
"emp_salary": 37500.56,
}
emp_skills is of an array type
6) Date
Date data type stores current date or time. There are various methods to return date. It can
be either as a string or as a date object. In the below table, we have discussed the methods
for the date.
ISODate() -It also returns a date object. Uses the ISODate() wrapper.
Example:
{
"emp_name": "Shobha",
"emp_skills”: [“React”,”Python”,”R”]
“emp_DOJ” : ISODate(“2001-09-27 T10:30:18.345Z)
}
7) Null
The NULL data type is used to represent a value of zero or no value, as the name implies.
{
"emp_name": "Shobha",
"emp_skills”: [“React”,” Python”,” R”],
“emp_award”: null
}
8) Object ID
An embedded document is a key-value pair that is put inside another document. Embedded
documents are stored using the object data type.
{
"emp_name": "Shobha",
"emp_dept”: {
“dept”: “MCA”,
“Semester”: 3,
“Subject”: “Machine Learning:
}
}
In MongoDB, data has a flexible schema. It is totally different from SQL database where you
had to determine and declare a table's schema before inserting data. MongoDB collections do
not enforce document structure.
The main challenge in data modeling is balancing the need of the application, the performance
characteristics of the database engine, and the data retrieval patterns.
The key challenge in MongoDB data modeling is balancing the requirements of the application.
Also, we need to assure the performance aspect effectively while modeling. Let’s point out
some requirements while MongoDB Data Modeling taking place.
a. Data Usage
While designing a data model, one must consider that how applications will access the
database. Also, what will be the pattern of data, such as reading, writing, updating, and deletion
of data. Some applications are read centric and some are write-centric.
There are possibilities that some data use frequently whereas some data is completely static.
We should consider these patterns while designing the schema.
b. Document growth
Some updates increase the size of the documents. During initialization, MongoDB assigns a
fixed document size. While using embedded documents, we must analyze if the sub object can
grow further out of bounds.
Otherwise, there may occur performance degradation when the size of the document crosses
its limit. MongoDB relocates the document on disk if the document size exceeds the allocated
space for that document.
c. Atomicity
Atomicity in contrast to the database means operations must fail or succeed as a single unit. If
a parent transaction has many sub-operations, it will fail even if a single operation fails.
Operations in MongoDB happen at the document level.
No single write operation can affect more than one collection. Even if it tries to affect multiple
collections, these will treat as separate operations. A single write operation can insert or update
the data for an entity. Hence, this facilitates atomic write operations.
However, schemas that provide atomicity in write operations may limit the applications to use
the data. It may also limit the ways to modify applications. This consideration describes the
challenge that comes in a way of data modeling for flexibility.
There can be two ways to establish relationships between the data in MongoDB:
• Referenced Documents
• Embedded Documents
a. Referenced Documents
Reference is one of the tools that store the relationship between data by including links from
one data to another. In such data, a reference to the data of one collection will be used to collect
the data between the collections.
We can say, applications resolve these references to access the related data. These are
normalized data models.
Reference relationships should be used to establish one to many or many to many relationships
between documents. Also, when the referenced entities are frequently updated or grow
indefinitely.
b. Embedded Documents
These can be considered as de-normalized data models. As the name suggests, embedded
documents create relationships between data by storing related data in a single document
structure. These data models allow applications to retrieve and manipulate related data in a
single database operation.
Embedded documents should be considered when the embedded entity is an integral part of the
document and not updated frequently. It should be used when there is a contained relation
between entities and they should not grow indefinitely.
Here, in MongoDB you don't need to create a database manually because MongoDB will create
it automatically when you save the value into the defined collection at first time. It don't need
to mention what you want to create, it will be automatically created at the time you save the
value into the defined collection.
Creating a Database
If there is no existing database, the following command is used to create a new database.
A number of databases can be run on a single MongoDB server. Default database of MongoDB
is 'db', which is stored within data folder.
MongoDB can create databases on the fly. It is not required to create a database before you
start working with it.
"show dbs" command provides you with a list of all the databases.
Using NodeJS
creatDB.js
>node test.js
Database created!
Collection in MongoDB
creatcollection.js
// A Client to MongoDB
var MongoClient = require('mongodb').MongoClient;
>node creatcollection.js
Collection created!
Step 1: Select a MongoDB database you like to Create a Collection within, using USE
command. Following is the syntax of USE command.
use <database_name>
Step 2: Insert a record to Collection, with Collection Name mentioned in the command as
shown below
db.<collection_name>.insert(<document>)
The collection should be created.
where
name [mandatory] name of collection
insertOne()
Use the db.<collection>.insertOne() method to insert a single document in a
collection. db points to the current database, <collection> is existing or a new collection
name.
Syntax:
db.collection.insertOne(document, [writeConcern])
Parameters:
1. document: A document to insert into the collection.
db.employees.insertOne({
name: "Shobha",
branch: "MCA",
salary:30000
})
Output
{
acknowledged: true,
insertedId: ObjectId("616d44bea861820797edd9b0")
}
In the above example, we passed a document to the insertOne() method. Notice that we
haven't specified _id field. So, MongoDB inserts a document to a collection with the auto-
generated unique _id field. It returns an object with a boolean field acknowledged that
indicates whether the insert operation is successful or not, and insertedId field with the newly
inserted _id value.
insertdata.js
// A Client to MongoDB
var MongoClient = require('mongodb').MongoClient;
insertdata.js
// A Client to MongoDB
var MongoClient = require('mongodb').MongoClient;
var myobj=[
{name: "Nikhil", dept: "ME", salary:20000},
{name: "Dhanush", dept: "AE", salary:56000},
{name: "Rachana", dept: "ME", salary:36000}
];
dbo.collection("students").insertMany(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
console.log("Number of documents inserted: " + res.insertedCount);
db.close();
});
});
Output:
Syntax:
db.collection.findOne(query, projection)
Parameters:
query: Optional. Specifies the criteria using query operators.
projection: Optional. Specifies the fields to include in a resulted document.
The findOne() returns the first document from a collection if no parameter is passed.
Example:
db.employees.findOne({name: "Shobha"})
Use the query operators for more refined search. For example, the following finds the first
document where salary is greater than 40000.
Output:
{
name: "Sudha",
branch: "Dean",
salary:50000
},
{
name: "Usha",
branch: "MBA",
salary:43000
}
finddata.js
// URL at which MongoDB service is running
var url = "mongodb://localhost:27017/";
// A Client to MongoDB
var MongoClient = require('mongodb').MongoClient;
finddata.js
// A Client to MongoDB
var MongoClient = require('mongodb').MongoClient;
db.close();
});
});
Output: