MongoDB Data Model 浅谈

MongoDB 对于数据的 schema 要求很灵活

与 MySQL 相比,collection 并不会强制文档的结构。(MySQL 在定义表时, 需要指定有哪些字段、类型、展示长度等)

因此,插入数据到 MongoDB 非常灵活。

同一个 collection 中可以有结构不同(字段不同)的文档(通常来说,文档结构还是相近的)。

 

MongDB 适合半结构化/非结构化的数据,适用场景为大数据分析,允许数据不那么精确;

MySQL 适合结构化的数据,支持数据引擎插拔;5.6+ 默认引擎为 InnoDB,支持事务。

 

MongoDB 有两种 Data Model:

  1. Embedded
  2. Reference

1. Embedded

反范式(denormalized).

用关系型数据库中的范式来看,违反了第一范式(1NF,属性不能拆分)。

增加了冗余,查询时不用连表。

2. Reference

类似外键(foreign key)。

符合范式(normalized)。

 

MongoDB 在确定 data model 时,需要考虑两点要素:

1、data 本身的特征;

2、MongoDB 本身的特征。

不同的 data model 会给应用带来不同的结果,比如:更高效的查询、更高的插入/更新吞吐量、更好的负载。

如果更新会频繁地造成文档增长,考虑使用 reference 而非 embedded。

读多写少,考虑使用 embedded,提高查询效率。

 

参考资料:

https://docs.mongodb.com/manual/core/data-models/

 

转载于:https://www.cnblogs.com/huangzejun/p/8882363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Focus on data usage and better design schemas with the help of MongoDB About This Book Create reliable, scalable data models with MongoDB Optimize the schema design process to support applications of all kinds Use this comprehensive guide to implement advanced schema designs Who This Book Is For This book is intended for database professionals, software developers, and architects who have some previous experience with MongoDB and now want to shift their focus to the concepts of data modeling. If you wish to develop better schema designs for MongoDB-based applications, this book is ideal for you. In Detail This book covers the basic concepts in data modeling and also provides you with the tools to design better schemas. With a focus on data usage, this book will cover how queries and indexes can influence the way we design schemas, with thorough examples and detailed code. The book begins with a brief discussion of data models, drawing a parallel between relational databases, NoSQL, and consequently MongoDB. Next, the book explains the most basic MongoDB concepts, such as read and write operations, indexing, and how to design schemas by knowing how applications will use the data. Finally, we will talk about best practices that will help you optimize and manage your database, presenting you with a real-life example of data modeling on a real-time logging analytics application. Table of Contents Chapter 1. Introducing Data Modeling Chapter 2. Data Modeling with MongoDB Chapter 3. Querying Documents Chapter 4. Indexing Chapter 5. Optimizing Queries Chapter 6. Managing the Data Chapter 7. Scaling Chapter 8. Logging and Real-time Analytics with MongoDB

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值