Skip to content

Commit 64c249e

Browse files
authored
Merge pull request #844 from Vipullakum007/mongo-basics-docs
mongoDB-basics added
2 parents d620353 + 5fedd10 commit 64c249e

8 files changed

+634
-0
lines changed

docs/MongoDB/_category.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "MongoDB",
3+
"position": 12,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "MongoDB is a popular NoSQL database known for its high performance, high availability, and easy scalability. It uses a flexible, JSON-like data model, which allows for the storage of complex data structures. MongoDB is designed to handle a wide variety of data types and workloads, making it a versatile choice for modern applications. Key features include document-oriented storage, powerful query capabilities, indexing, replication, and sharding. MongoDB is widely used in various industries for applications requiring fast development cycles, large-scale data processing, and real-time analytics."
7+
}
8+
}

docs/MongoDB/create-database.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: mongodb-create-database
3+
title: MongoDB - Create Database
4+
sidebar_label: Create Database
5+
sidebar_position: 6
6+
tags: [mongodb, create database, commands]
7+
description: Learn how to create a database in MongoDB using the use command, along with examples and additional commands.
8+
---
9+
10+
# MongoDB - Create Database
11+
12+
In this chapter, we will see how to create a database in MongoDB.
13+
14+
## The `use` Command
15+
16+
The `use DATABASE_NAME` command in MongoDB is used to create a database. The command will create a new database if it doesn't exist; otherwise, it will return the existing database.
17+
18+
### Syntax
19+
20+
The basic syntax of the `use DATABASE` statement is as follows:
21+
22+
```sql
23+
use DATABASE_NAME
24+
```
25+
26+
### Example
27+
28+
If you want to use a database named `mydb`, the `use DATABASE` statement would be as follows:
29+
30+
```bash
31+
> use mydb
32+
switched to db mydb
33+
```
34+
35+
To check your currently selected database, use the command `db`:
36+
37+
```bash
38+
> db
39+
mydb
40+
```
41+
42+
If you want to check your databases list, use the command `show dbs`:
43+
44+
```bash
45+
> show dbs
46+
local 0.78125GB
47+
test 0.23012GB
48+
```
49+
50+
Your created database (`mydb`) is not present in the list. To display the database, you need to insert at least one document into it:
51+
52+
```bash
53+
> db.movie.insert({"name":"tutorials point"})
54+
> show dbs
55+
local 0.78125GB
56+
mydb 0.23012GB
57+
test 0.23012GB
58+
```
59+
60+
In MongoDB, the default database is `test`. If you didn't create any database, then collections will be stored in the `test` database.
61+
62+
## Data Modeling Diagram
63+
64+
```mermaid
65+
graph TD
66+
A[Create Database] --> B[use Command]
67+
B --> C[Syntax]
68+
B --> D[Example]
69+
D --> E[Switch Database]
70+
D --> F[Check Current Database]
71+
D --> G[Check Databases List]
72+
D --> H[Insert Document]
73+
D --> I[Display Database]
74+
```
75+
76+
## Command Summary
77+
78+
| Command | Description |
79+
|-----------------------------------|--------------------------------------------------------------|
80+
| `use DATABASE_NAME` | Create or switch to a database |
81+
| `db` | Check the currently selected database |
82+
| `show dbs` | List all databases |
83+
| `db.<collection>.insert(document)`| Insert a document into a collection |

docs/MongoDB/data-modeling.md

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
id: mongodb-data-modeling
3+
title: MongoDB - Data Modelling
4+
sidebar_label: Data Modelling
5+
sidebar_position: 5
6+
tags: [mongodb, data modeling, schema design]
7+
description: Learn about data modeling in MongoDB, including embedded and normalized data models, with examples and considerations for schema design.
8+
---
9+
10+
# MongoDB - Data Modelling
11+
12+
Data in MongoDB has a flexible schema. Documents in the same collection do not need to have the same set of fields or structure. Common fields in a collection’s documents may hold different types of data.
13+
14+
## Data Model Design
15+
16+
MongoDB provides two types of data models: Embedded data model and Normalized data model. Based on the requirement, you can use either of the models while preparing your document.
17+
18+
### Embedded Data Model
19+
20+
In this model, you can have (embed) all the related data in a single document. It is also known as the de-normalized data model.
21+
22+
For example, assume we are getting the details of employees in three different documents, namely, Personal_details, Contact, and Address. You can embed all three documents into a single one as shown below:
23+
24+
```json
25+
{
26+
"_id": "ObjectId",
27+
"Emp_ID": "10025AE336",
28+
"Personal_details": {
29+
"First_Name": "Radhika",
30+
"Last_Name": "Sharma",
31+
"Date_Of_Birth": "1995-09-26"
32+
},
33+
"Contact": {
34+
"email": "[email protected]",
35+
"phone": "9848022338"
36+
},
37+
"Address": {
38+
"city": "Hyderabad",
39+
"Area": "Madapur",
40+
"State": "Telangana"
41+
}
42+
}
43+
```
44+
45+
### Normalized Data Model
46+
47+
In this model, you can refer to the sub-documents in the original document using references. For example, you can rewrite the above document in the normalized model as:
48+
49+
#### Employee
50+
51+
```json
52+
{
53+
"_id": "<ObjectId101>",
54+
"Emp_ID": "10025AE336"
55+
}
56+
```
57+
58+
#### Personal_details
59+
60+
```json
61+
{
62+
"_id": "<ObjectId102>",
63+
"empDocID": "<ObjectId101>",
64+
"First_Name": "Radhika",
65+
"Last_Name": "Sharma",
66+
"Date_Of_Birth": "1995-09-26"
67+
}
68+
```
69+
70+
#### Contact
71+
72+
```json
73+
{
74+
"_id": "<ObjectId103>",
75+
"empDocID": "<ObjectId101>",
76+
"email": "[email protected]",
77+
"phone": "9848022338"
78+
}
79+
```
80+
81+
#### Address
82+
83+
```json
84+
{
85+
"_id": "<ObjectId104>",
86+
"empDocID": "<ObjectId101>",
87+
"city": "Hyderabad",
88+
"Area": "Madapur",
89+
"State": "Telangana"
90+
}
91+
```
92+
93+
## Considerations while Designing Schema in MongoDB
94+
95+
1. **Design your schema according to user requirements.**
96+
2. **Combine objects into one document if you will use them together. Otherwise, separate them (but make sure there is no need for joins).**
97+
3. **Duplicate the data (but limited) because disk space is cheap compared to compute time.**
98+
4. **Do joins while writing, not on reading.**
99+
5. **Optimize your schema for the most frequent use cases.**
100+
6. **Do complex aggregation in the schema.**
101+
102+
## Example
103+
104+
Suppose a client needs a database design for their blog/website. Let's see the differences between RDBMS and MongoDB schema design. The website has the following requirements:
105+
106+
- Every post has a unique title, description, and URL.
107+
- Every post can have one or more tags.
108+
- Every post has the name of its publisher and the total number of likes.
109+
- Every post has comments given by users along with their name, message, date-time, and likes.
110+
- On each post, there can be zero or more comments.
111+
112+
### RDBMS Schema Design
113+
114+
In RDBMS schema design, for the above requirements, you will have at least three tables.
115+
116+
### MongoDB Schema Design
117+
118+
In MongoDB schema design, you will have one collection called `post` with the following structure:
119+
120+
```json
121+
{
122+
"_id": "POST_ID",
123+
"title": "TITLE_OF_POST",
124+
"description": "POST_DESCRIPTION",
125+
"by": "POST_BY",
126+
"url": "URL_OF_POST",
127+
"tags": ["TAG1", "TAG2", "TAG3"],
128+
"likes": "TOTAL_LIKES",
129+
"comments": [
130+
{
131+
"user": "COMMENT_BY",
132+
"message": "TEXT",
133+
"dateCreated": "DATE_TIME",
134+
"like": "LIKES"
135+
},
136+
{
137+
"user": "COMMENT_BY",
138+
"message": "TEXT",
139+
"dateCreated": "DATE_TIME",
140+
"like": "LIKES"
141+
}
142+
]
143+
}
144+
```
145+
146+
So, while showing the data, in RDBMS you need to join three tables, but in MongoDB, data will be shown from one collection only.
147+
148+
### Data Modeling Diagram
149+
150+
```mermaid
151+
graph TD
152+
A[Data Model Design] --> B[Embedded Data Model]
153+
A --> C[Normalized Data Model]
154+
B --> D[Single Document]
155+
C --> E[Separate Documents]
156+
E --> F[References]
157+
```
158+
159+
### Data Modeling Summary
160+
161+
| Feature | Embedded Data Model | Normalized Data Model |
162+
|---------------------------------|-----------------------------|-----------------------------|
163+
| **Data Storage** | Single document | Separate documents |
164+
| **Schema Complexity** | Simple | Complex |
165+
| **Performance** | Fast reads | Fast writes |
166+
| **Use Case** | High read/write frequency | Large data with references |
167+
168+
MongoDB provides a flexible and efficient way to model your data based on your application needs. Choosing the right data model depends on the use case and access patterns.

docs/MongoDB/drop-database.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
id: mongodb-drop-database
3+
title: MongoDB - Drop Database
4+
sidebar_label: Drop Database
5+
sidebar_position: 7
6+
tags: [mongodb, drop database, commands]
7+
description: Learn how to drop a database in MongoDB using the dropDatabase() command, along with examples and additional commands.
8+
---
9+
10+
# MongoDB - Drop Database
11+
12+
In this chapter, we will see how to drop a database using the MongoDB command.
13+
14+
## The `dropDatabase()` Method
15+
16+
The MongoDB `db.dropDatabase()` command is used to drop an existing database.
17+
18+
### Syntax
19+
20+
The basic syntax of the `dropDatabase()` command is as follows:
21+
22+
```sql
23+
db.dropDatabase()
24+
```
25+
26+
This will delete the selected database. If you have not selected any database, then it will delete the default `test` database.
27+
28+
### Example
29+
30+
First, check the list of available databases by using the command `show dbs`:
31+
32+
```bash
33+
> show dbs
34+
local 0.78125GB
35+
mydb 0.23012GB
36+
test 0.23012GB
37+
```
38+
39+
If you want to delete the new database `mydb`, the `dropDatabase()` command would be as follows:
40+
41+
```bash
42+
> use mydb
43+
switched to db mydb
44+
> db.dropDatabase()
45+
{ "dropped" : "mydb", "ok" : 1 }
46+
```
47+
48+
Now check the list of databases:
49+
50+
```bash
51+
> show dbs
52+
local 0.78125GB
53+
test 0.23012GB
54+
```
55+
56+
## Dropping Database Diagram
57+
58+
```mermaid
59+
graph TD
60+
A[Drop Database] --> B[dropDatabase Method]
61+
B --> C[Syntax]
62+
B --> D[Example]
63+
D --> E[Check Available Databases]
64+
D --> F[Switch to Database]
65+
D --> G[Drop Database]
66+
D --> H[Verify Database Removal]
67+
```
68+
69+
## Command Summary
70+
71+
| Command | Description |
72+
|-------------------------|-------------------------------------------------------|
73+
| `db.dropDatabase()` | Drop the currently selected database |
74+
| `show dbs` | List all databases |
75+
| `use DATABASE_NAME` | Switch to a specific database |

docs/MongoDB/home.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
id: mongodb-home
3+
title: MongoDB Tutorial Home
4+
sidebar_label: Home
5+
sidebar_position: 1
6+
tags: [mongodb, nosql, database]
7+
description: In this tutorial, you will learn about MongoDB, its concepts, architecture, and applications, enabling you to create and deploy highly scalable and performance-oriented databases.
8+
---
9+
10+
# MongoDB Tutorial
11+
12+
MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you great understanding on MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database.
13+
14+
### Audience
15+
16+
This tutorial is designed for Software Professionals who are willing to learn MongoDB Database in simple and easy steps. It will throw light on MongoDB concepts and after completing this tutorial you will be at an intermediate level of expertise, from where you can take yourself at higher level of expertise.
17+
18+
### Prerequisites
19+
20+
Before proceeding with this tutorial, you should have a basic understanding of database, text editor and execution of programs, etc. Because we are going to develop high performance database, so it will be good if you have an understanding on the basic concepts of Database (RDBMS).

0 commit comments

Comments
 (0)