MongoDB 数据建模

版权所有,未经许可,禁止转载

章节


MongoDB中的数据结构很灵活。同一集合中的文档,数据结构不必相同。

MongoDB中设计数据结构时的一些注意事项

  • 根据用户需求设计数据结构。
  • 如果需要把对象组合在一起使用,例如:文章与评论,可将它们放到一个文档中。
  • 可以适当有数据冗余,因为与计算时间相比,磁盘空间更便宜。

例子

假设我们需要为一个博客网站设计数据库。博客网站有以下要求:

  • 每个帖子都有唯一的标题、描述和url。
  • 每个帖子可以有一个或多个标签。
  • 每一篇文章都有其发布者的名字和总点赞数。
  • 每个帖子都有用户评论,评论包括姓名、评论内容、评论时间和点赞数。
  • 每个帖子都可以有0条或更多评论。

在关系数据库中,针对上述需求的设计,将至少有三个表。

image

而在MongoDB中,将有一个集合post,结构如下

{
   _id: POST_ID
   title: TITLE_OF_POST, 
   description: POST_DESCRIPTION,
   by: POST_BY,
   url: URL_OF_POST,
   tags: [TAG1, TAG2, TAG3],
   likes: TOTAL_LIKES, 
   comments: [	
      {
         user:'COMMENT_BY',
         message: TEXT,
         dateCreated: DATE_TIME,
         like: LIKES 
      },
      {
         user:'COMMENT_BY',
         message: TEXT,
         dateCreated: DATE_TIME,
         like: LIKES
      }
   ]
}

因此,在显示数据时,在关系数据库中,需要连接三个表,在MongoDB中,只需读取一个集合中的数据。

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值