01 - Introduction to MongoDB
01 - Introduction to MongoDB
What is MongoDB?
Starting from this session, we are going to learn all about MongoDB starting from scratch. So, first thing that comes to mind is that what is MongoDB, s
let’s get started:
What is MongoDB ?
MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relation
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 is a database which came into lig
around the mid-2000s.
Now, let’s see some features of MongoDB:
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. 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.
5. 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.
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.
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.
JavaScript Object Notation, more commonly known as JSON, was defined as part of the JavaScript language in the early 2000s by JavaScript creato
Douglas Crockford, though it wasn’t until 2013 that the format was officially specified.
JavaScript objects are simple associative containers, wherein a string key is mapped to a value (which can be a number, string, function, or even anothe
object). This simple language trait allowed JavaScript objects to be represented remarkably simply in text as shown below:
{
"_id": 1,
"name" : { "first" : "John"
"John",
, "last" : "Backus" },
"contribs" : [ "Fortran",
"Fortran", "ALGOL"
"ALGOL",, "Backus-Naur Form",
Form", "FP" ],
"awards" : [
{
"award" : "W.W. McDowell Award",
Award",
1967,
"year" : 1967 ,
"by" : "IEEE Computer Society"
}, {
"award" : "Draper Prize",
Prize",
1993,
"year" : 1993 ,
"by" : "National Academy of Engineering"
}
]
}
As JavaScript became the default language of client-side web development, JSON began to take on a life of its own. JSON quickly moved beyond th
web page, and into software everywhere.
JSON shows up in many different cases:
APIs
Configuration files
Log messages
Database storage
BSON:
BSON simply stands for “Binary JSON,” and that’s exactly what it was invented to be. BSON’s binary structure encodes type and length informatio
which allows it to be parsed much more quickly.
Since its initial formulation, BSON has been extended to add some optional non-JSON-native data types, like dates and binary data, without whic
MongoDB would have been missing some valuable support.
Languages that support any kind of complex mathematics typically have different sized integers (ints vs longs) or various levels of decimal precisio
(float, double, decimal128, etc.).
Not only is it helpful to be able to represent those distinctions in data stored in MongoDB, it also allows for comparisons and calculations to happe
directly on data in ways that simplify consuming application code.
Now, a question arises that whether MongoDB uses BSON or JSON:
MongoDB stores data in BSON format both internally, and over the network, but that doesn’t mean you can’t think of MongoDB as a JSON databas
Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.
Unlike systems that simply store JSON as string-encoded values, or binary-encoded blobs, MongoDB uses BSON to offer the industry’s most powerf
indexing and querying features on top of the web’s most usable data format.
Let’s try to get a gist about MongoDB through an example:
The below example shows how a document can be modeled in MongoDB.
1. The _id field is added by MongoDB to uniquely identify the document in the collection.
2. What you can note is that the Order Data (OrderID, Product, and Quantity ) which in RDBMS will normally be stored in a separate table,
while in MongoDB it is actually stored as an embedded document in the collection itself. This is one of the key differences in how data is
modeled in MongoDB.
Key Components of MongoDB Architecture
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. So
for example, if we see the example of the above customer table, Mongo DB will add a 24 digit unique identifier to each document in the
collection.
The first 4 bytes representing the seconds since the unix epoch
MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document. The comple
combination of ObjectId makes all the _id fields unique.
Creating New ObjectId
To generate a new ObjectId use the following code −
>newObjectId = ObjectId()
ObjectId("5349b4ddd2781d08c09890f3")
Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id −
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
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.
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.
2. Once download is complete open the msi file. Click Next in the start up screen
3. Accept the End-User License Agreement and click Next.
4. Click on the “complete” button to install all of the components. The custom option can be used to install selective components or if you
want to change the location of the installation.
5. Select “Run service as Network Service user”. make a note of the data directory, we’ll need this later and click Next.
8. Final step, Once complete the installation, Click on the Finish button
Now, after installing MongoDB on our computer we will install MongoDB Compass - a management tool for MongoDB.
Note: While downloading it might ask for details so fill all the details and click submit, then downloading will start.
2. Double click on the downloaded file
Now, as we are in the MongoDB Shell, now we are ready to create our new database:
Creating a Database:
A database is a place where data is stored in an organized way. In MongoDB, databases are used to store collections. A single MongoDB server ca
have multiple databases and a single MongoDB database can have multiple collections.
You can use MongoDB Shell or MongoDB Compass to create a new database.
Using MongoDB Shell:
MongoDB provides the use <database-name command to connect with the database. If the specified database name does not exist then it creates
and set it as a current database.
Syntax
Example
If you want to use a database with name , then use DATABASE statement would be as follows −
>use humanResourceDb
switched to db humanResourceDb
>db
humanResourceDb
You can create a new database using MongoDB Compass. For that, open Compass and connect with your local or remote database. Once it connec
with the MongoDB server, click on the top "CREATE DATABASE" button which will open the popup window, as shown below.
Enter your database name and collection name and click Create Database. This will create a new database humanResourceD with th
new employees collection shown below.
Droping a Database:
MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax
db.
db.dropDatabase
dropDatabase(
()
This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.
If you want to delete new database , then dropDatabase() command would be as follows −
>use humanResourceDb
switched to db humanResourceDb
db.
>db.dropDatabase
dropDatabase(
()
"humanResourceDb",
>{ "dropped" : "humanResourceDb", "ok" : 1 }
>
db.
db.createCollection
createCollection((collection_name
collection_name,, options
options)
)
Here, db.createCollection() is the method used; "name" is a data type - string which is specifying the name of the collection to be formed. "options" is a
added document type which helps in specifying the size of memory along with indexing the collection in the database. This parameter is an optional one
db.
db.createCollection
createCollection(("MyCollection"
"MyCollection")
)
Output:
Example:
db.
db.movie
movie.
.insert
insert(
({"name":"Avengers: Endgame"}
"name": Endgame"})
The above operation will automatically create a collection if the collection with this name does not currently exist. Also, if you want to check an inserte
document, you can use the find() method. Its syntax is:
Syntax:
db.
db.collection_name
collection_name.
.find
find(
()
Output:
Syntax:
db.
db.collection_name
collection_name.
.drop
drop(
()
Continuing from the example in the previous lesson where you have created a collection inside the my_project_db database. Now use this database
remove or drop the movie collection and see the changes:
Example:
use my_project_db
db.
db.movie
movie.
.drop
drop(
()
show collections
Output:
Conclusion:
In this session, we have learnt about:
What is MongoDB ?
Advantages of using MongoDB
Interview Questions:
Is MongoDB better than SQL databases ? If yes then how ?
MongoDB is better than other SQL databases because it allows a highly flexible and scalable document structure
structure.
.
For example:**
**For
**
- One data document in MongoDB can have five columns and the other one in the same collection can have ten columns.
columns.
- MongoDB database are faster than SQL databases due to efficient indexing and storage techniques.
techniques.
MongoDB stores data in BSON format both internally, and over the network, but that doesn’t mean you can’t think of Mongo
Unlike systems that simply store JSON as string-encoded values, or binary-encoded blobs, MongoDB uses BSON to offer the
Thank You