---
disqus: hackmd
---
Introduction to NoSQL Databases <br> WEEK_2 - Introducing MongoDB
====
###### tags: `IBM Data Engineering Professional Certificate`,`Reading Note`,`Coursera`,`Introduction to NoSQL Databases`
### Overview
Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.
## Basic of MongoDB
### 1. Overview of MongoDB
* What is MongoDB Database?
* MongoDB 是一個 document 型和 NoSQL 的資料庫。
* 資料是有架構但沒關聯的型態。
* What are documents?
* documents 會以 JSON 或 Python dictionaries 存在。
* 以 student document 作為範例:
<br>
* What is a Collection?
* 由一群 document 組成。
<br>
* What is a Database?
* 有一群 Collection 組成。
* Documents in detail
<br>
<br>
* 時間儲存格式為 ISODate。
* 數字可儲存為 Integer, Float。
* 提供 dictionaries 及 lists 的儲存格式。
* Why use MongoDB?
* 比起 RDBMS 讀寫資料更為直接。
* Where to use MongoDB
* 選用 MongoDB 的理由
* Large and unstructured
* Complex
* Flexible
* Highly scalable applications
* Self-managed, hybrid, or cloud hosted
### 2. Advantages of MongoDB
* 補充:
* Schema 通常是指「資料表及欄位的設計」,泛指資料表欄位的數量,資料型態,大小長度,主鍵,外鍵,複合鍵,唯一性,是否可為null,索引等。
* Flexibility with Schema
<br>
* 資料架構彈性化。
* Code-first Approach
| Relational databases | MongoDB |
| :---: | :---: |
| 資料庫預先設計 | 無需事先定義 Table |
| 寫程式 | 完成連接資料庫就可開始寫程式 |
| <br> | <br> |
* Evolving Scheme
<br>
* 可以隨時新增資料。
* Unstructured Data
<br>
* 沒有固定的資料格式。
* Querying and Analytics
* MongoDB 採用 MQL 訪問資料。
* High Availability
* MongoDB is natively a highly available system:
* 資料庫系統彈性化。
* 沒有系統維護停機時間。
* 沒有系統升級停機時間。
### 3. Use Cases for MongoDB
* Many Sources - One View
* No more data silos
* Easy data ingestion
* Consolidate different data
* Flexible schema
* Internet of Things (IoT)
* Billions of IoT devices around the world
* Vast amount of data
* Scale
* Expressive querying
* E-commerce
* Products with different attributes
* Optimise for Read
* Dynamic schema
<br>
* Real_time Analytics
* Quick response to changes
* Simplified ETL
* Real time, along with Operational Data
* Gaming
* Globally scalable
* No downtime
* Supporting rapid development
* Finance
* Speed
* Security
* Reliability
## Getting Started with MongoDB
### 1. CRUD Operations
* Mongo Shell
* 連結資料庫
<br>
* 設定資料庫
<br>
* Create
<br>
* db 是以上所設定的 campusManagementDB。
* stdent 是 db 裡的 collection。
* insertOne 是 mongDB 的 function。
<br>
* mongoDB 輸出結果。
* Create Many
<br>
* 以上是輸入多筆資料範例。
* Read
* findOne()
<br>
* 輸出 collection 中的一份 document。
<br>
* 使用關鍵字輸出第一筆資料資料。
* find()
<br>
* count()
<br>
* Replace
<br>
* Upadate
<br>
* Delete
<br>
### 2. Indexes
* When to Index
* Most frequant queries
<br>
* use Index to Sort
* Indexes can help with sorting
<br>
* Indexes in MongoDB
* MongoDB indexes are speacial data structures.
* They store the fields you are indexing.
* They also store the location of document.
* How MongoDB Stores Indexes
<br>
### 3. Aggregation Framework
* Agregation Stages
<br>
* Common Aggregation Stages
* $project
<br>
* &sort
<br>
* $count
<br>
* $merge
<br>
### 4. Replication & Sharding
### 5. Accessing MongoDB from Python
* MongoClient
* A class that helps you interact with MongoDB
<br>
* Create
<br>
* Create Many
<br>
* Read
<br>
* Print read documents
<br>
* Replace
<br>
* Update
<br>
* Delete
<br>
## Summary and Highlights
* 課程完整整理的內容,所以把它記錄下來
* Basic of MongoDB
>* MongoDB is a document and NoSQL database.
>* It is easy to access by indexing.
>* It supports various data types, including dates and numbers.
>* The database schema can be flexible when working with MongoDB.
>* You can change the database schema as needed without involving complex data definition language statements.
>* Complex data analysis can be done on the server using Aggregation Pipelines.
>* The scalability MongoDB provides makes it easier to work across the globe.
>* MongoDB enables you to perform real-time analysis on your data.
* Getting Started with MongoDB
>* CRUD operations consist of Create, Read, Update, and Delete.
>* The Mongo shell is an interactive command line tool provided by MongoDB to interact with your databases.
>* Indexes help quickly locate data without looking for it everywhere.
>* MongoDB stores data being indexed on the index entry and a location of the document on disk.
>* Using an aggregation framework, you can perform complex analysis on the data in MongoDB.
>* You can build your aggregation process in stages such as match, group, project, and sort.
>* Replication is the duplication of data and any changes made to the data.
>* Replication provides fault tolerance, redundancy, and high availability for your data.
>* For growing data sets, you can use sharding to scale horizontally.
>* MongoClient is a class that helps you interact with MongoDB.