1-MongoDB (3 Files Merged)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Back End Technologies

==================

MongoDB Database
Node.js Server Side Technology
Express Middleware

Database
========
- Application needs data.
- Data can be stored in a file or in database.
- Relational Data and Scalable Data
- Various database
- Oracle
- MySql
- Sql Server
- MongoDB

- MongoDB is document based database


- MongoDB is non-sql [no-sql] database
- It is JavaScript based.
- Data in JSON
- It is schema less
- Data Replications
- High Availability
- Ad-hoc querying
- Indexing
- Not good for Analytics
- Not good for Predictions

Setup Environment for MonogDB:


1. Download and Install MongoDB
5x

https://www.mongodb.com/try/download/community

2. Install the ".msi"

3. Start MongoDB Database Server

run => services.msc => MongoDB server => Right Click => Start

4. Connect to server by using

a) Command Line Tool


b) GUI Tool called "MongoDB Compass"

Command Line Tool


================
1. Open Command Prompt
2. Change to the following location

C:\program files\mongodb\server\5.0\bin> mongo.exe

MongoDB Compass
================
1. open MongoDB Compass from programs

2. Select New Connection

3. Define connection string

mongodb://127.0.0.1:27017

4. click "connect"

- The reference for creating database for any project is


"Database Model"

- Database Models are classified into

1. Conceptual Model
2. Physical Model
3. Logical Model
4. Entity Model [ER Diagram]
MongoDB Commands
[CRUD]
C - Create
R - Read
U - Update
D - Delete

RDBMS MongoDB
[Oracle|MySql]
--------------------------------------------
Database Database

Tables Collections

Records Documents

Fields Fields

Joins Embeded Documents

1. Connect to MongoDB Server from Client

C:\program files\mongodb\server\5.0\bin> mongo.exe

2. Commands

> cls [clear screen]


> ctrl + c [close - disconnect]
> exit [ close command line]

3. Database Commands [Case Sensitive]

> show dbs [to display list of database]

> db [to view the active database]

> use dbname [to change into exisitng database]


[to create a new database]

Creating a new Database


----------------------------------
>use databaseName

To Remove any Database


-----------------------------------
> use databaseName
> db.dropDatabase()

Note: Database will not display in list if there are no collections.

Creating a new Collection:


-----------------------------------
> db.createCollection("collectionName", { options })

> db.createCollection("tblcategories");
> db.createCollection("tblproducts", { })
option type description
---------------------------------------------------------------------
autoIndexId boolean It will add an auto id when set to true.

max number max number of records allowed.

capped boolean true, it will replace old with new.

size number space allocated in memory.


[bytes]

> db.createCollection("tblproducts")
> db.createCollection("tblcategories", {autoIndexId:true, max:10,
capped:true, size:2097152})

To View the Collections in Database


------------------------------------------------
> show collections

To Remove any Collection


-----------------------------------
> db.collectionName.drop()

Inserting Documents into collection


-----------------------------------------------

> db.collectionName.insertOne({ fieldName:value, field:value })


> db.collectionName.insertMany([{}, { }, { }])

Note: all are JavaScript type.

Ex:
> db.tblcategories.insertOne({CategoryId:1, CategoryName:"Electronics"})

> db.tblcategories.insertMany([{CategoryId:2, CategoryName:"Footwear"},


{CategoryId:3, CategoryName:"Fashion"}])

Ex: All JavaScript data type are same here in MongoDB

> db.tblproducts.insertOne({ProductId:1, Name:"Samsung TV",


Price:56000.33, Stock:true, Mfd:new Date("2022-03-22"),
Cities:["Delhi","Hyd"], Rating:{rate:3.4, count:3000}})

Reading Document from Collection


-----------------------------------------------

> db.tblproducts.find({ query }) // returns all documents

> db.tblproducts.find().pretty()
Querying

{ProductId:2} Equal To
{Name:"Nike Casuals"}
{Price: {$gt:10000} } Greater than
{Price: {$gte:10000} } Greater than or equal
{Price: {$lt:10000} } Less than
{Price: {$lte:10000} } Less than or equal
{Price: {$ne:10000} } Not equal
Querying Document
- find({ })
- Query Operators
$gt >
$lt <
$gte >=
$lte <=
$ne !=
$in find value in array, property in object
$and AND
$or OR
$not NOT

{
$and:[{Price:{$gt:4000}}, {Price:{$lt:10000}},
{Category:"Electronics"}]
}

Ex:
>
db.tblproducts.find({$and:[{Price:{$gt:4000}},{Price:{$lt:10000}}]}).pret
ty()

Ex: Find in Array

> db.tblproducts.find({Cities:{$in:["Hyd"]}}).pretty()

Update Queries
---------------------
updateOne()
updateMany()

Operators:
$set : changes the value
$rename : change the field name
$unset : remove the field

Syntax:
db.collection.updateOne({findQuery}, { updateQuery})

Ex:
> db.tblproducts.updateOne({ProductId:3},{$set:{Price:3500.55}})
> db.tblproducts.updateMany({}, {$set:{Stock:true}})

EX:
updateMany(
{
$and:[ {Price:{$gt:5000}}, {Price:{$lt:10000}} ]
},
{
$set: {Stock:true}
}
)

Ex: Rename Fields

> db.tblproducts.updateMany({},{$rename:{Cities:"ShippedTo"}})
> db.tblproducts.updateMany({},{$unset:{Cities:" "}})

Delete Query
-------------------
deleteOne()
deleteMany()

Syntax:
db.tblproducts.deleteOne({findQuery});

db.tblproducts.deleteMany({ShippedTo:{$in:["Delhi"]}})

db.tblcategories.deleteOne({CategoryId:3})

IEEE Project
[ I-Shop - Shopping ]

https://ieeexplore.ieee.org/document/7941952
https://sci-hub.hkvisa.net/

- Create a Word Document with following details

1. Abstract
3 to 4 line summary

2. Existing System
points

3. Proposed System
points

4. Modules used in project


9 modules

5. Prepare Database Models


a) Conceptual - which module needs data
b) Physical - define table name, field name, data type
c) Logical - relation between modules
d) Entity - table, field, data type, relations

Node.js and Express

You might also like