Professional Documents
Culture Documents
Unit-IV
Unit-IV
MongoDB
Dr. C. Raghavendra
Associate Professor,
Department of Emerging Technologies,
CVR College of Engineering, HYD. 1
Contents
2
Introduction to MongoDB
• MongoDB, the most popular NoSQL (Not only SQL)
database, is an open-source document-oriented database
that provides high performance, high availability, and
automatic scaling.
• NoSQL databases are quite useful for working with large sets of
distributed data.
5
Features of MongoDB
1. Support ad hoc queries
2. Indexing
• A master can perform Reads and Writes and a Slave copies data from
the master and can only be used for reads or back up (not writes)
4. Duplication of data
10. Stores files of any size easily without complicating your stack.
8
Cont.
11. Easy to administer in the case of failures.
11
How to run the shell
• To start the shell, open command prompt, run it as a
administrator then run the mongo executable:
$ mongo
>x= 100
100
>x/ 5;
20
13
Cont.
• You can also use the JavaScript libraries
Hello, MongoDB!
• When you press "Enter", the shell detect whether the JavaScript
statement is complete or not.
• If you press "Enter" three times in a row, it will cancel the half-
formed command and get you back to the > prompt.
14
Databases and Collections
• MongoDB stores data records as documents (specifically
BSON documents) which are gathered together in
collections.
15
Cont.
• To select a database to use, in mongosh, issue the use <db> statement, as
in the following example:
use myDB
Create a Database
• If a database does not exist, MongoDB creates the database when you first
store data for that database. As such, you can switch to a non-existent
database and perform the following operation in mongosh:
use myNewDB
db.myNewCollection1.insertOne( { x: 1 } )
16
Cont.
• The insertOne() operation creates both the database
myNewDB and the collection myNewCollection1 if they do
not already exist.
17
Cont.
Collections
18
Database, Collection, Document
19
Cont.
Database:
• Each of the databases has its own set of files on the file
system with multiple databases existing on a single
MongoDB server.
20
Cont.
Collection:
22
Ex:
23
Comparing MongoDB vs SQL
24
Cont.
Document:
25
Cont.
26
Cont.
27
Note
Primary key
28
Sharding
• Sharding is a method for distributing a single dataset across
multiple databases, which can then be stored on multiple
machines.
32
Replication and Sharding
• Replication: The primary server node copies data onto
secondary server nodes. This can help increase data
availability and act as a backup, in case if the primary
server fails.
33
• Advantages of sharding:
• High availability
• Disadvantages of sharding:
• Query overhead
• Complexity of administration
2. Read Operation
3. Update Operation
4. Delete Operation
35
1. Create Operations
I. db.collection.insertOne()
II. db.collection.insertMany()
36
Cont.
• In MongoDB, insert operations target a single collection. All
write operations in MongoDB are atomic on the level of a
single document.
37
Ex:
Insert a Single Document
db.inventory.insertOne(
)
38
Cont.
• To retrieve the document that you just inserted, query the collection:
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" }},
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom:
"cm" } }
])
39
Read Operations
• Read operations retrieve documents from a collection.
db.collection.find()
40
Cont.
41
Update Operations
• Update operations modify existing documents in a
collection. MongoDB provides the following methods to
update documents of a collection:
1. db.collection.updateOne()
2. db.collection.updateMany()
3. db.collection.replaceOne()
42
Cont.
• In MongoDB, update operations target a single collection.
43
Delete Operations
• Delete operations remove documents from a collection.
• db.collection.deleteOne()
• db.collection.deleteMany()
44
Cont.
• In MongoDB, delete operations target a single collection.
45
Real Time Database Firebase CRUD operations
46
Cont.
• As our application evolves over time, it’s recommended to
build an app with Firebase and change the schema
simultaneously based on the requirements.
47
Cont.
• What Are We Building?
48
• We just need the 6 steps below in order to achieve the final outcome:
• STEP #3: Initialize firebase into the app by adding the code snippet
• STEP #4: Enable read and write permission to the firebase database
• STEP #5: Import users schema JSON file into the database
• STEP #6: Read user’s data from the firebase using child_added()
method
49
Thank You
50