Skip to content

mongoDB-basics added #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/MongoDB/_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "MongoDB",
"position": 12,
"link": {
"type": "generated-index",
"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."
}
}
83 changes: 83 additions & 0 deletions docs/MongoDB/create-database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
id: mongodb-create-database
title: MongoDB - Create Database
sidebar_label: Create Database
sidebar_position: 6
tags: [mongodb, create database, commands]
description: Learn how to create a database in MongoDB using the use command, along with examples and additional commands.
---

# MongoDB - Create Database

In this chapter, we will see how to create a database in MongoDB.

## The `use` Command

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.

### Syntax

The basic syntax of the `use DATABASE` statement is as follows:

```sql
use DATABASE_NAME
```

### Example

If you want to use a database named `mydb`, the `use DATABASE` statement would be as follows:

```bash
> use mydb
switched to db mydb
```

To check your currently selected database, use the command `db`:

```bash
> db
mydb
```

If you want to check your databases list, use the command `show dbs`:

```bash
> show dbs
local 0.78125GB
test 0.23012GB
```

Your created database (`mydb`) is not present in the list. To display the database, you need to insert at least one document into it:

```bash
> db.movie.insert({"name":"tutorials point"})
> show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
```

In MongoDB, the default database is `test`. If you didn't create any database, then collections will be stored in the `test` database.

## Data Modeling Diagram

```mermaid
graph TD
A[Create Database] --> B[use Command]
B --> C[Syntax]
B --> D[Example]
D --> E[Switch Database]
D --> F[Check Current Database]
D --> G[Check Databases List]
D --> H[Insert Document]
D --> I[Display Database]
```

## Command Summary

| Command | Description |
|-----------------------------------|--------------------------------------------------------------|
| `use DATABASE_NAME` | Create or switch to a database |
| `db` | Check the currently selected database |
| `show dbs` | List all databases |
| `db.<collection>.insert(document)`| Insert a document into a collection |
168 changes: 168 additions & 0 deletions docs/MongoDB/data-modeling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
id: mongodb-data-modeling
title: MongoDB - Data Modelling
sidebar_label: Data Modelling
sidebar_position: 5
tags: [mongodb, data modeling, schema design]
description: Learn about data modeling in MongoDB, including embedded and normalized data models, with examples and considerations for schema design.
---

# MongoDB - Data Modelling

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.

## Data Model Design

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.

### Embedded Data Model

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.

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:

```json
{
"_id": "ObjectId",
"Emp_ID": "10025AE336",
"Personal_details": {
"First_Name": "Radhika",
"Last_Name": "Sharma",
"Date_Of_Birth": "1995-09-26"
},
"Contact": {
"email": "radhika_sharma.123@gmail.com",
"phone": "9848022338"
},
"Address": {
"city": "Hyderabad",
"Area": "Madapur",
"State": "Telangana"
}
}
```

### Normalized Data Model

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:

#### Employee

```json
{
"_id": "<ObjectId101>",
"Emp_ID": "10025AE336"
}
```

#### Personal_details

```json
{
"_id": "<ObjectId102>",
"empDocID": "<ObjectId101>",
"First_Name": "Radhika",
"Last_Name": "Sharma",
"Date_Of_Birth": "1995-09-26"
}
```

#### Contact

```json
{
"_id": "<ObjectId103>",
"empDocID": "<ObjectId101>",
"email": "radhika_sharma.123@gmail.com",
"phone": "9848022338"
}
```

#### Address

```json
{
"_id": "<ObjectId104>",
"empDocID": "<ObjectId101>",
"city": "Hyderabad",
"Area": "Madapur",
"State": "Telangana"
}
```

## Considerations while Designing Schema in MongoDB

1. **Design your schema according to user requirements.**
2. **Combine objects into one document if you will use them together. Otherwise, separate them (but make sure there is no need for joins).**
3. **Duplicate the data (but limited) because disk space is cheap compared to compute time.**
4. **Do joins while writing, not on reading.**
5. **Optimize your schema for the most frequent use cases.**
6. **Do complex aggregation in the schema.**

## Example

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:

- Every post has a unique title, description, and URL.
- Every post can have one or more tags.
- Every post has the name of its publisher and the total number of likes.
- Every post has comments given by users along with their name, message, date-time, and likes.
- On each post, there can be zero or more comments.

### RDBMS Schema Design

In RDBMS schema design, for the above requirements, you will have at least three tables.

### MongoDB Schema Design

In MongoDB schema design, you will have one collection called `post` with the following structure:

```json
{
"_id": "POST_ID",
"title": "TITLE_OF_POST",
"description": "POST_DESCRIPTION",
"by": "POST_BY",
"url": "URL_OF_POST",
"tags": ["TAG1", "TAG2", "TAG3"],
"likes": "TOTAL_LIKES",
"comments": [
{
"user": "COMMENT_BY",
"message": "TEXT",
"dateCreated": "DATE_TIME",
"like": "LIKES"
},
{
"user": "COMMENT_BY",
"message": "TEXT",
"dateCreated": "DATE_TIME",
"like": "LIKES"
}
]
}
```

So, while showing the data, in RDBMS you need to join three tables, but in MongoDB, data will be shown from one collection only.

### Data Modeling Diagram

```mermaid
graph TD
A[Data Model Design] --> B[Embedded Data Model]
A --> C[Normalized Data Model]
B --> D[Single Document]
C --> E[Separate Documents]
E --> F[References]
```

### Data Modeling Summary

| Feature | Embedded Data Model | Normalized Data Model |
|---------------------------------|-----------------------------|-----------------------------|
| **Data Storage** | Single document | Separate documents |
| **Schema Complexity** | Simple | Complex |
| **Performance** | Fast reads | Fast writes |
| **Use Case** | High read/write frequency | Large data with references |

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.
75 changes: 75 additions & 0 deletions docs/MongoDB/drop-database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
id: mongodb-drop-database
title: MongoDB - Drop Database
sidebar_label: Drop Database
sidebar_position: 7
tags: [mongodb, drop database, commands]
description: Learn how to drop a database in MongoDB using the dropDatabase() command, along with examples and additional commands.
---

# MongoDB - Drop Database

In this chapter, we will see how to drop a database using the MongoDB command.

## The `dropDatabase()` Method

The MongoDB `db.dropDatabase()` command is used to drop an existing database.

### Syntax

The basic syntax of the `dropDatabase()` command is as follows:

```sql
db.dropDatabase()
```

This will delete the selected database. If you have not selected any database, then it will delete the default `test` database.

### Example

First, check the list of available databases by using the command `show dbs`:

```bash
> show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
```

If you want to delete the new database `mydb`, the `dropDatabase()` command would be as follows:

```bash
> use mydb
switched to db mydb
> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
```

Now check the list of databases:

```bash
> show dbs
local 0.78125GB
test 0.23012GB
```

## Dropping Database Diagram

```mermaid
graph TD
A[Drop Database] --> B[dropDatabase Method]
B --> C[Syntax]
B --> D[Example]
D --> E[Check Available Databases]
D --> F[Switch to Database]
D --> G[Drop Database]
D --> H[Verify Database Removal]
```

## Command Summary

| Command | Description |
|-------------------------|-------------------------------------------------------|
| `db.dropDatabase()` | Drop the currently selected database |
| `show dbs` | List all databases |
| `use DATABASE_NAME` | Switch to a specific database |
20 changes: 20 additions & 0 deletions docs/MongoDB/home.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: mongodb-home
title: MongoDB Tutorial Home
sidebar_label: Home
sidebar_position: 1
tags: [mongodb, nosql, database]
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.
---

# MongoDB Tutorial

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.

### Audience

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.

### Prerequisites

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).
Loading
Loading