0% found this document useful (0 votes)
0 views8 pages

MongoDB_Introduction

MongoDB notes

Uploaded by

amol nagrale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

MongoDB_Introduction

MongoDB notes

Uploaded by

amol nagrale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

MongoDB Database

Introduction
MongoDB, the most popular NoSQL database, is an open-source document-oriented
database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on
the table-like relational database structure but provides an altogether different mechanism
for storage and retrieval of data. This format of storage is called BSON ( similar to JSON
format).
A simple MongoDB document Structure:
{
Name: 'Student',
Address: 'Pune',
Hobby: 'Cricket',
type: 'NoSQL'
}

SQL databases store data in tabular format. This data is stored in a predefined data model
which is not very much flexible for today’s real-world highly growing
applications. Modern applications are more networked, social and interactive than
ever. Applications are storing more and more data and are accessing it at higher rates.
Relational Database Management System(RDBMS) is not the correct choice when it
comes to handling big data by the virtue of their design since they are not horizontally
scalable. If the database runs on a single server, then it will reach a scaling limit. NoSQL
databases are more scalable and provide superior performance. MongoDB is such a NoSQL
database that scales by adding more and more servers and increases productivity with its
flexible document model.

RDBMS vs MongoDB:
 RDBMS has a typical schema design that shows number of tables and the
relationship between these tables whereas MongoDB is document-oriented.
There is no concept of schema or relationship.
 Complex transactions are not supported in MongoDB because complex join
operations are not available.
 MongoDB allows a highly flexible and scalable document structure. For
example, one data document of a collection in MongoDB can have two fields
whereas the other document in the same collection can have four.
 MongoDB is faster as compared to RDBMS due to efficient indexing and
storage techniques.
 There are a few terms that are related in both databases. What’s called Table in
RDBMS is called a Collection in MongoDB. Similarly, a Row is called a
Document and a Column is called a Field. MongoDB provides a default ‘_id’ (if
not provided explicitly) which is a 12-byte hexadecimal number that assures the
uniqueness of every document. It is similar to the Primary key in RDBMS.
Features of MongoDB:
 Document Oriented: MongoDB stores the main subject in the minimal number
of documents and not by breaking it up into multiple relational structures like
RDBMS. For example, it stores all the information of a computer in a single
document called Computer and not in distinct relational structures like CPU,
RAM, Hard disk, etc.
 Indexing: Without indexing, a database would have to scan every document of a
collection to select those that match the query which would be inefficient. So,
for efficient searching Indexing is a must and MongoDB uses it to process huge
volumes of data in very less time.
 Scalability: MongoDB scales horizontally using sharding (partitioning data
across various servers). Data is partitioned into data chunks using the shard key,
and these data chunks are evenly distributed across shards that reside across
many physical servers. Also, new machines can be added to a running database.
 Replication and High Availability: MongoDB increases the data availability
with multiple copies of data on different servers. By providing redundancy, it
protects the database from hardware failures. If one server goes down, the data
can be retrieved easily from other active servers which also had the data stored
on them.
 Aggregation: Aggregation operations process data records and return the
computed results. It is similar to the GROUPBY clause in SQL. A few
aggregation expressions are sum, avg, min, max, etc

Where do we use MongoDB?


MongoDB is preferred over RDBMS in the following scenarios:
 Big Data: If you have huge amount of data to be stored in tables, think of
MongoDB before RDBMS databases. MongoDB has built-in solution for
partitioning and sharding your database.
 Unstable Schema: Adding a new column in RDBMS is hard whereas MongoDB
is schema-less. Adding a new field does not effect old documents and will be
very easy.
 Distributed data Since multiple copies of data are stored across different
servers, recovery of data is instant and safe even if there is a hardware failure.

Local Setup for MongoDB: -


1. Steps for Running MongoDB As a docker Container:-
Step 1: Pull the MongoDB container.
Cmd: - docker pull mongo

Step 2: Run the container and provide the username and password.
Cmd: - docker run -d -p 27017:27017 --name some-mongo -e
MONGO_INITDB_ROOT_USERNAME=mongoadmin -e
MONGO_INITDB_ROOT_PASSWORD=bdung mongo:latest

Step 3: Verify the state.


Cmd: - docker ps

Step 4: Access the MongoDB shell in the running container


Cmd: - docker exec -it 029f612c14af /bin/bash

Step 5: Provide username and password to for login


Cmd:- mongosh -u mongoadmin -p bdung

Note :- If you want to exit from Mongodb


Cmd: - exit
2. Steps to perform CRUD operations on Mongodb:-
a. To see all databases
Cmd:- show dbs

b. Create a new database called “mongopractise”


Cmd: - use [database_name]

Note: - In MongoDB, a database is not actually created until it gets content.

c. To create a collection
Cmd:- db.createCollection(‘ [collection_name]’ );

d. To see the collections (ie. Tables)


Cmd:- show collections

e. To delete the collections:


Cmd :- db.[collection_name].drop()

f. To insert the data into the collection, there are two methods:-
Method 1:- To insert a single document, use the insertOne() method.
This method inserts a single object into the database.
Cmd:- db.[collection_name].insertOne({data:data})

Method 2: - To insert multiple documents at once, use the insertMany() method.


This method inserts an array of objects into the database.
Cmd: - db.[collection_name].insertMany([{data1:data1}, {data2:data2},
{data3:data3}])
g. To find the data(or to see the data in the table). There are two method for finding
the data
Method 1: - To retrieve data from a collection in MongoDB, we can use the find()
method. This method accepts a query object. If left empty, all documents will be
returned.
Cmd:- db.[collection_name].find()

Method 2: - To select only one document, we can use the findOne() method. This
method accepts a query object. If left empty, it will return the first document it
finds. This method only returns the first match it finds.
Cmd: - db.[collection_name].findOne()

h. To find particular data


To query, or filter, data we can include a query in our find() or findOne() methods.
Cmd: - db.[collection_name].find({data:data})

i. To see particular field in the collection


Both find methods accept a second parameter called projection. This parameter is
an object that describes which fields to include in the results. This parameter is
optional. If omitted, all fields will be included in the results.
Cmd: - db.[collection_name].find({}, {data_show:1, data_not_show:0})
Note: - “1” means to show the data, and “0” mean do not show the data of that
field.

j. To update an existing document we can use updateOne() or updateMany()


methods.The updateOne() method will update the first document that is found
matching the provided query.

Cmd: - db.[collection_name].updateOne({find_collection:data}, {$set:


{set_new_data : add_newdata } } )

In this way we can update the data.

If the data is not found to update it, It will add the data by using “upsert”
To update many field
The updateMany() method will update all documents that match the provided
query.
k. To Delete documents
We can delet documents by using the method deleteOne() or deleteMany().
The deleteOne() method will delete the first document that matches the query
provided.
Cmd: - db.[collection_name].deleteOne({data:data})

The deleteMany() method will delete all documents that match the query
provided.
Cmd: - db.[collection_name].deleteMany({data:data})

Note: -
Reference dock link- MongoDB mongosh Delete (w3schools.com)

You might also like