0% found this document useful (0 votes)
6 views5 pages

MongoDB NoSQL

This document provides a step-by-step guide for creating and managing a MongoDB database named 'companydb' with an 'employee' collection. It includes instructions for inserting documents, querying data, updating documents, and understanding key NoSQL concepts. Additionally, it highlights the flexibility of document-oriented databases compared to traditional SQL databases.

Uploaded by

Deep Narayan
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)
6 views5 pages

MongoDB NoSQL

This document provides a step-by-step guide for creating and managing a MongoDB database named 'companydb' with an 'employee' collection. It includes instructions for inserting documents, querying data, updating documents, and understanding key NoSQL concepts. Additionally, it highlights the flexibility of document-oriented databases compared to traditional SQL databases.

Uploaded by

Deep Narayan
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

STEP 5 — Create Database

Click:
Create Database
Enter:
Database Name:
companydb
Collection Name:
employee
Click Create.
--------------------------------------------------
STEP 6 — Insert First Document
Inside employee collection:
Click:
Add Data
→ Insert Document
Paste:
{
"empno": 1001,
"name": "John",
"dept": "HR",
"salary": 5000,
"skills": ["Excel","SQL"]
}
Click Insert.
--------------------------------------------------
STEP 7 — Insert More Documents
Add:
{
"empno": 1002,
"name": "Ravi",
"dept": "IT",
"salary": 7000,
"skills": ["Python","MongoDB"]
}

And:
{
"empno": 1003,
"name": "Asha",
"dept": "Finance",
"salary": 6500
}
--------------------------------------------------
STEP 8 — Query Data
At top there is:
Filter
Use queries.
Find IT Employees:
{ "dept": "IT" }
Click Find.
Salary > 6000:
{
"salary": { "$gt": 6000 }
}
--------------------------------------------------
STEP 9 — Update Document
Open John document.
Click Edit.
Add:
"city": "Patna"
Save.
Observe:
No schema change required.
--------------------------------------------------
STEP 10 — Insert Nested JSON
Insert:
{
"empno": 1004,
"name": "Neha",
"address": {
"city": "Delhi",
"state": "Delhi"
}
}
--------------------------------------------------
STEP 11 — Query Nested Data
Filter:
{
"[Link]": "Delhi"
}
This demonstrates document-oriented structure.
--------------------------------------------------
STEP 12 — Observe Key NoSQL Ideas
SQL Thinking vs MongoDB Thinking
Table → Collection
Row → Document
ALTER TABLE → Usually unnecessary
Strict schema → Flexible schema
Relational → Document-oriented
--------------------------------------------------
STEP 13 — Optional Mini Challenge
Insert:
{
"name":"Kiran",
"projects":["ETL","BigQuery","Power BI"]
}
Then query:
{
"projects":"Power BI"
}
--------------------------------------------------
MOST IMPORTANT LEARNING GOAL
Understand this deeply:

Different documents can have different fields.


That is one of the biggest differences from Oracle/PostgreSQL.

From Mongo Shell

--------------------------------------------------
1. Show All Databases
--------------------------------------------------

show dbs

--------------------------------------------------
2. Switch/Create Database
--------------------------------------------------

use companydb

--------------------------------------------------
3. Show Current Database
--------------------------------------------------

db

--------------------------------------------------
4. Show Collections (like tables)
--------------------------------------------------

show collections

--------------------------------------------------
5. Insert One Document
--------------------------------------------------

[Link]({
empno:1001,
name:"John",
dept:"HR",
salary:5000
})

--------------------------------------------------
6. Insert Multiple Documents
--------------------------------------------------

[Link]([
{
empno:1002,
name:"Ravi",
dept:"IT",
salary:7000
},
{
empno:1003,
name:"Asha",
dept:"Finance",
salary:6500
}
])

--------------------------------------------------
7. View All Documents
--------------------------------------------------

[Link]()

--------------------------------------------------
8. Pretty Format Output
--------------------------------------------------

[Link]().pretty()

--------------------------------------------------
9. Filter Records
--------------------------------------------------

[Link]({dept:"IT"})

--------------------------------------------------
10. Salary Greater Than 6000
--------------------------------------------------

[Link]({
salary: {$gt:6000}
})

--------------------------------------------------
11. Select Specific Fields
--------------------------------------------------

[Link](
{},
{name:1,salary:1,_id:0}
)

--------------------------------------------------
12. Update One Document
--------------------------------------------------

[Link](
{name:"John"},
{$set:{salary:5500}}
)

--------------------------------------------------
13. Add New Field
--------------------------------------------------

[Link](
{name:"John"},
{$set:{city:"Patna"}}
)

--------------------------------------------------
14. Delete One Document
--------------------------------------------------

[Link]({name:"Asha"})

--------------------------------------------------
15. Count Documents
--------------------------------------------------

[Link]()

--------------------------------------------------
16. Sort Data
--------------------------------------------------

[Link]().sort({salary:-1})

(-1 = descending)
(1 = ascending)

--------------------------------------------------
17. Limit Rows
--------------------------------------------------

[Link]().limit(2)

--------------------------------------------------
18. Find Nested JSON
--------------------------------------------------

[Link]({
"[Link]":"Delhi"
})

--------------------------------------------------
19. Drop Collection
--------------------------------------------------

[Link]()

--------------------------------------------------
20. Drop Database
--------------------------------------------------
[Link]()

You might also like