Jiayong
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- 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.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully