Logo
Section 1 / 6

数据建模简介

了解 MongoDB 中数据建模的基础知识,重点学习如何通过嵌入式文档和引用文档的方式来组织数据,以提升性能和可扩展性。

导入

数据建模简介

了解 MongoDB 中数据建模的基础知识,重点学习如何通过嵌入式文档和引用文档的方式来组织数据,以提升性能和可扩展性。

1 2 { 3 "id": "0001", 4 "name": "Allison Hill", 5 "email": "jillrhodes@miller.com", 6 "passwordHash": "hashed_password_1", 7 "address": [ 8 { 9 "street": "908 Jennifer Squares", 10 "city": "Robinsonshire", 11 "postalCode": "36964", 12 "country": "Sudan" 13 } 14 ], 15 ... 16 }
1 2"orderHistory": [ 3 { 4 "id": "3000" 5 }, 6 { 7 "id": "3001" 8 }, 9 { 10 "id": "3002" 11 }, 12 { 13 "id": "3003" 14 } 15]
1 2{ 3 "id": "3001", 4 "userId": "0001", 5 "orderDate": "2024-11-21T06:32:47.383235", 6 "items": [ 7 { 8 "productId": 2049, 9 "quantity": 3, 10 "price": 103 11 }, 12 { 13 "productId": 2083, 14 "quantity": 3, 15 "price": 46 16 } 17 ], 18 "totalAmount": 447, 19 "paymentMethod": "Credit Card", 20 "status": "Cancelled" 21 }

表格

策略描述使用场景
嵌入式将相关数据嵌套在父文档内部。我们经常一起访问这些数据(例如:商品与评论)。
引用式在另一个集合中存储指向相关文档的 ID。数据在多个地方重复使用,或数据量过大不适合嵌入。

性能优化中的索引

MongoDB 中的数据验证——JSON Schema