You are on page 1of 6

MongoDB

Introduction What is NoSQL ?

- MongoDB is a document – oriented database, - Data ?


used for a high volume of data storage Any information that is useful to us is known as
- It is an open – source, NoSQL database. data
- MongoDB was founded in 2007 by Dwight - Database ?
Merriman, Eliot Horowitz, and Kevin Ryan. It A database is a set of related data organized in
was developed by 10gen such a way that all the data can be accessed easily
- Why MongoDB from it
1. All the modern applications require big data, - DBMS ?
faster development of features and flexible Is the software used for storing and retrieving this
deployment. It was developed to make sure data
that following points are taken care of - Different databases management (Hierarchical
2. It able to develop faster and the deployment database vs. Relational database)
process is a lot easier a. Hierarchical database
3. It also able to cater to the Big data 1) The data in this structure is stored in a
requirement parent – children relationship node
4. E – commerce website eBay, mobile wallet 2) In a hirerchical database, besides actual
PayPal and online photo storage website data, records also contain information
shuuterfly are some of the well – known about their groups of parent / child
examples of MongoDB users relationships
3) Is a like a tree structure
4) The data in this structure is stored in a
parent – children relationship node
5) In a hierarchical database, besides actual
data, records also contain information
about their groups of parent / child
relationships
Example

A. Shoes
a) Women’s shoes
1. High heels
2. Bellies
b) Men’s shoes
1. Sports shoes
2. Sneakers

Explaination : Shoes category is the


parent and the children are Women’s
shoes and Men’s shoes. In a similar way,
women’s shoes and high – heels have a
parent – child relationship

6) The IBM (Information Management


System) and Windows Registry are two
popular examples of hierarchical
databases

b. Relational database
1) This database is based on the relational
data model, which stores data in the form
of rows and columns
2) These rows and column together forms a
table (relation)
3) Many modern database systems like MS
SQL server, oracle, MySQL, and MS
access use RDBMS
4) Relational database management systems
that uses SQL are schema – oriented i.e.
the structure of the data should be known
in advance
- NoSQL databases
a. NoSQL databases are the databases that do
not use SQL as their primary data access
language
b. NoSQL database does not have predefined
schemas like RDBMS, which makes NoSQL
database a perfect candidate for rapidly
changing development environments
c. NoSQL allows developers to make changes
on the fly without affecting applications
d. In short NoSQL is like your favourite
superhero who is not needed to abide by the
traditional database laws. And it makes and
follows laws of its own
e. There are various sub – types of NoSQL
database, out of which MongoDB is based on
document type NoSQL database
f. So coming to document databases
(documentdb), they are NoSQL databases that
store data in the form of documents, each
document represents the data, its relationship
between other data elements, and attributes of
data.
g. Document database store data in a key value
form

car=“Mercedes”

here the key is “car”, and it’s value is


“Mercedes”

h. When is NoSQL used ?


1) When you want to store and retrieve huge
amouts of data NoSQL is your best friend
2) When the data is not structured and
changing over time
3) The data is growing continuously and you
need to scale the database regularly to
handle the data
How to install MongoDB Database through documents and collections in
MongoDB
- MongoDB supports a variety of OS
- What will you need to install ? - Database, collections and documents are the three
1. A windows desktop most important words in MongoDB
2. Working internet connection to download - In MongoDB, the first basic step is to have a
the .exe file database and collection in place
- Step - (1) the database is used to store all of the
1. Step 1 : go to the MongoDB website and collections
download the community server - (2) the collection is used to store all of the
documents
https://www.mongodb.com/try/downloa - (3) the documents will contain the relevant useful
d/community information
- Think as cupboard !
2. Step 2 : once the download is completed,
open the downloaded MSIfile “Think of your database as a cupboard. A
3. Step 3 : on the next screen, accept the terms cupboard has may racks or partitions. Thinks of
and click on next each rack in the cupboard as a collection. There
4. Step 4 : now select the complete installation can be many collections in a database. In each
5. Step 5 : select “Run service as a network” and rack you can keep multiple items, i.e in each
click on Next collection you can store multiple documents.”
6. Step 6 : click on the install button, wait for the
installation to complete - Components
7. Step 7 : and you are done, click on finish 1) MongoDB database – can contain one or
- Launch MongoDB more collections
1. So you have seen how to install MongoDB, 2) Collections – can contain differenttypes of
time to launch MongoDB so that we can learn document (objects)
how to use various features of MongoDB 3) Document – key value pair list or array or
2. Open CMD > change the directory to the nested document
destination where the bin folder of MongoDB - MongoDB document
is present. This can be done by using the a. In MongoDB, all the data is stored in
following command in CMD: individual documents
b. These documents are in the form of a BSON
cd C:\ProgramFiles\MongoDB\Server\4.2\bin (Binary Javascript Object Notation)
c. BSON is something similar to JSON which
3. To start the MongoDB server, in cmd use the has an attribute – value pair
command : d. Example

mongod {
name: ‘Jane’,
4. To enter the MongoDB shell, use the result: ‘Pass’,
command score: ‘85’,
}
mongo
Explanation:
5. And you are up and running with MongoDB.  The above document contains information
We’ll look at how data is stored and accessed of a student named Jane, where her result
in MongoDB and score are mentioned
 A document starts and ends with curly
brackets ‘{’ and ‘}’
 In this example, there are three keys
namely ‘name’, ‘result’ and ‘score’
 Each key has a value associated with it.
these key – value pairs form a basic
document in MongoDB. The values can
be written in single quotes or double
quotes
 Whenever there is more than one data that
needs to be entered in a document, we use
a comma to separate the individual data

- MongoDB collection
a. Documents are part of a collection
b. When two or more documents are present in a
file, the file becomes a collection
c. How to name a collection ?
1) Collection names must begin with letters
or an underscore. So you can write a
collection name as “pro_collection” or
“_procollection”
2) A collection name may contain numbers.
So this is also valid as “pro_collection1”
3) You can’t use the “$” character within the
name of a collection. “$” is reserved. So a
collection name like “pro$collection” will
be invalid
4) A collection name must not exceed 128
characters. Nobody likes long names!
JSON and BSON! The difference Time to Create and Drop databases and collections

- JSON - How to create a database in MongoDB


a. JSON also is known as JavaScript Object a. It may sound funny, but there is no command
Notation is an open standard file format and to create a database in MongoDB
data interchange format. b. Actually, MongoDB does not provide any
b. JSON uses human – readable text to store and command to create a database
transmit data objects consisting of attribute – c. In MongoDB you don’t need to create a
value pairs and array data types database manually because MongoDB will
c. Attribute – value pair can also be know as create it automatically when you save the
Name – Value pair value into the defined collection at first time
d. Example d. You also don’t need to mention that you want
to create a database. Once you store the value
{ in the collection your database is created
“name” : “Kevin”, automatically
“result” : “Pass” e. When there is no existing database in
} MongoDB you can create a new database by
using the below command
Note* : above is a small example of how a
JSON file looks, the “name” and the “result” Syntax:
are the Keys of the JSON and “Kevin” and use DATABASE_NAME
“Pass” are the values of the keys respectively
this syntax helps in following scenarios
e. You have seen above a JSON file has an 1) When you want to create and enter a new
Attribute – value pair. We can see that an database which is not present previously
attribute – value pair consists of a field name 2) When you to want enter an already
(in double quotes), followed by a colon, existing database
followed by a value
Example
- BSON
a. JSON and BSON are close cousins, as their command prompt – mongo
nearly identical names imply > use test_db
b. The name “BSON” is based on the term Switched to db test_db
JSON and stands for “Binary JSON” >
c. In order to make MongoDB JSON – first, but
still high – performance and general – f. What can we do to see that we have entered
purpose, BSON was invented the correct database ? To check the currently
d. MongoDB stores data in BSON format both selected database we can use the command
internally, and over the network, but that
doesn’t mean you can’t think of MongoDB as Syntax :
a JSON database. db
e. Anything you can represent in JSON can be
natively stored in MongoDB, and retrieved Example
just as easily in JSON
f. So when you write data in MongoDB in the >db
form of a JSON, MongoDB converts that data Test_db
into a BSON >
g. So if you know JSON, you can easily start
with MongoDB note* : above, you can see that we have
h. You don’t have to take any trouble to convert entered the database – test_db, which is the
a JSON to BSON, MongoDB does that for correct database
you
g. Before creating a database, what if we want to
see the list of already existing databases ?

To check the database list i.e. all the


databases that are present, use the command

Syntax :
show dbs

Example

>show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
>

note* : above, we can see that “test_db” is not


present thus we can create the database for
our use

- How to drop a database in MongoDB


a. Dropping a database means deleting the
databse and also deleting all the associated
data files
b. The command used for deleting a database is
– dropDatabase
Syntax :
db.dropDatabase( )

c. This is a simple command that deletes the


current database that user is in
d. To use this command, make sure that you are
in the database that you want to delete. For
example if we want to delete the database
“test_db” we will follow the following steps:
Steps:
1) We will enter the database

>use test_db

2) Once we have entered the database, next


we will drop the database

>db.dropDatabase()

3) When the result is displayed as {“ok”:1},


this means that the operation was
successful

You might also like