Skip to content

Unit 03 - MongoDB Data Modeling

Difference from relational database model and the document model

How to model Data in MongoDB for your application

  • What are the requirements of your application?
  • What data will you be storing?
  • How will users access the data?
  • What data will be most valuable to your users?
  • What are the relationships between the data?

A good data model should

  • easier to manage data
  • make queries more efficient
  • use less memory and cpu

In MongoDB

  • data that is accessed together should be stored together
  • embedded documents enables us to build complex relationships between data
  • normalization data by using database references
  • store, query, and use resources efficiently

在MongoDB與關係型數據庫的差異:

特性MongoDB (NoSQL)關聯式資料庫 (RDBMS)
資料模型文件、集合和字段;可嵌套資料表、行和列;規範化資料
模式靈活,無固定模式固定模式,需要預先定義
可擴展性設計用於橫向擴展(分片)主要縱向擴展,橫向擴展較複雜
交易支援ACID,通常限於單文件或單集合完全支援ACID,可跨多表
查詢語言類JSON語法,特定於MongoDBSQL,較為標準化
資料關係通過嵌入文件或引用實現通過外鍵和連接實現
一致性通常優化為最終一致性強一致性
適用場景非結構化數據、高擴展性需求、快速開發結構化數據、複雜關係、強一致性需求
索引支持多種類型索引支持多種類型索引
模式變更靈活,易於適應變化通常需要複雜的遷移過程

這個表格概括了MongoDB(作為NoSQL資料庫的代表)和關聯式資料庫之間的主要差異。每種類型的資料庫都有其優勢和適用場景,選擇哪一種取決於具體的應用需求。