--- 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>![](https://i.imgur.com/3N4aESw.png) * What is a Collection? * 由一群 document 組成。 <br>![](https://i.imgur.com/gNzj9xv.png =200x) * What is a Database? * 有一群 Collection 組成。 * Documents in detail <br>![](https://i.imgur.com/118BekM.png) <br>![](https://i.imgur.com/qK25PPg.png) * 時間儲存格式為 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>![](https://i.imgur.com/3xocgkk.png) * 資料架構彈性化。 * Code-first Approach | Relational databases | MongoDB | | :---: | :---: | | 資料庫預先設計 | 無需事先定義 Table | | 寫程式 | 完成連接資料庫就可開始寫程式 | | <br>![](https://i.imgur.com/7kYSLMp.png) | <br>![](https://i.imgur.com/cjWjnLg.png) | * Evolving Scheme <br>![](https://i.imgur.com/S246f8u.png =500x) * 可以隨時新增資料。 * Unstructured Data <br>![](https://i.imgur.com/EsKmY4s.png =500x) * 沒有固定的資料格式。 * 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>![](https://i.imgur.com/TEoZJcz.png =500x) * 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>![](https://i.imgur.com/fZZucdd.png =500x) * 設定資料庫 <br>![](https://i.imgur.com/j87aqa4.png =500x) * Create <br>![](https://i.imgur.com/R1onvnG.png =500x) * db 是以上所設定的 campusManagementDB。 * stdent 是 db 裡的 collection。 * insertOne 是 mongDB 的 function。 <br>![](https://i.imgur.com/TGmVa9A.png =500x) * mongoDB 輸出結果。 * Create Many <br>![](https://i.imgur.com/28M1sQA.png =500x) * 以上是輸入多筆資料範例。 * Read * findOne() <br>![](https://i.imgur.com/6fIM8Yq.png =500x) * 輸出 collection 中的一份 document。 <br>![](https://i.imgur.com/ggrHhDt.png =500x) * 使用關鍵字輸出第一筆資料資料。 * find() <br>![](https://i.imgur.com/8wPYrYI.png =500x) * count() <br>![](https://i.imgur.com/eRfCgKn.png =500x) * Replace <br>![](https://i.imgur.com/1f3oz4z.png =500x) * Upadate <br>![](https://i.imgur.com/5kSvmUv.png =500x) * Delete <br>![](https://i.imgur.com/hAyhdu4.png =500x) ### 2. Indexes * When to Index * Most frequant queries <br>![](https://i.imgur.com/4ylN7MY.png =500x) * use Index to Sort * Indexes can help with sorting <br>![](https://i.imgur.com/jyvfSt6.png =500x) * 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>![](https://i.imgur.com/exzqV6H.png =500x) ### 3. Aggregation Framework * Agregation Stages <br>![](https://i.imgur.com/CBzdFME.png =500x) * Common Aggregation Stages * $project <br>![](https://i.imgur.com/exbvl8g.png =500x) * &sort <br>![](https://i.imgur.com/FW9GRoG.png =500x) * $count <br>![](https://i.imgur.com/3UJVeL2.png =500x) * $merge <br>![](https://i.imgur.com/j6DHvE5.png =500x) ### 4. Replication & Sharding ### 5. Accessing MongoDB from Python * MongoClient * A class that helps you interact with MongoDB <br>![](https://i.imgur.com/JBOAk1s.png =500x) * Create <br>![](https://i.imgur.com/RSxnloa.png =500x) * Create Many <br>![](https://i.imgur.com/9DoDiJL.png =500x) * Read <br>![](https://i.imgur.com/7li0ORS.png =500x) * Print read documents <br>![](https://i.imgur.com/QNwXcgJ.png =500x) * Replace <br>![](https://i.imgur.com/ayvxzib.png =500x) * Update <br>![](https://i.imgur.com/Boyo5AA.png =500x) * Delete <br>![](https://i.imgur.com/ZZrcr94.png =500x) ## 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.