Module 01 · Introducing NoSQL
Reading: MongoDB Design PatternsMongoDB 设计模式
MongoDB design patterns help optimize data models based on application queries and usage. However, MongoDB design patterns improve application performance by reducing schema complexity. It helps identify data storage patterns and the type of data returned to the application.
MongoDB 设计模式有助于根据应用程序查询和使用情况优化数据模型。然而,MongoDB 设计模式通过减少模式复杂性来提高应用程序性能。它有助于识别数据存储模式以及返回给应用程序的数据类型。
However, MongoDB design patterns are best practices for structuring data and queries to optimize performance, scalability, and maintainability. These patterns arise from common use cases and challenges encountered. Some prominent MongoDB design patterns include approximation, attribute, polymorphic, outliers, and bucket. Let's read about each of these MongoDB design patterns.
然而,MongoDB 设计模式是构建数据和查询以优化性能、可扩展性和可维护性的最佳实践。这些模式源于常见的用例和挑战。一些著名的 MongoDB 设计模式包括近似、属性、多态、异常值和分桶。让我们逐一阅读这些 MongoDB 设计模式。
Approximation 近似
Approximation MongoDB design pattern helps calculate frequent expenses, where the precision for those calculations is not a priority. For example, Instagram followers are for celebrities.
近似 MongoDB 设计模式有助于计算频繁性支出,其中这些计算的精度不是优先考虑的。例如,Instagram 粉丝是用于名人的。
The approximation design pattern is useful for fewer writes to the database and maintains statistically valid numbers. However, it cannot represent exact numbers.
近似设计模式对于减少对数据库的写入次数和保持统计有效数字是有用的。然而,它不能表示精确的数字。
Attribute 属性
The Attribute MongoDB design pattern helps document various characteristics and similar fields that you cannot identify when raising a query. This attribute is useful when the fields need to be sorted and found in a small subset of documents or when both conditions meet within the documents, such as when selling products on e-commerce.
属性 MongoDB 设计模式有助于记录各种特征和相似字段,这些字段在提出查询时无法识别。当字段需要被排序并在文档的小子集中找到,或者当文档中同时满足这两个条件时,这种属性非常有用,例如在电子商务上销售产品时。
The attribute MongoDB design pattern is useful in less indexing and generating simple and faster queries.
属性 MongoDB 设计模式在减少索引和生成简单快速的查询方面是有用的。
Polymorphic 多态
The polymorphic MongoDB design pattern helps store different documents in the same collection. However, it often uses a discriminator field to differentiate between types of documents, such as customer communication stored using various channels.
多态的 MongoDB 设计模式有助于将不同的文档存储在同一个集合中。然而,它通常使用一个区分字段来区分不同类型的文档,例如使用各种渠道存储的客户沟通。
This type of design pattern is easy to implement and runs queries across a single collection.
这种设计模式易于实现,并且可以在单个集合中运行查询。
Outlier 异常值
The Outlier MongoDB design pattern is useful for queries or documents that don't fit into the typical data pattern, such as a list of a celebrity's social media followers (which could be in millions). This design pattern prevents documents or queries from determining an application's solution; however, it is tailored for typical use cases but doesn't address ad hoc queries.
MongoDB 的异常值设计模式适用于不符合典型数据模式的查询或文档,例如名人的社交媒体粉丝列表(可能包含数百万)。这种设计模式防止文档或查询决定应用程序的解决方案;然而,它针对典型用例进行了优化,但无法处理临时查询。
Bucket Pattern 桶模式
The bucket pattern in MongoDB design helps to manage streaming data such as time series, real-time analytics, or Internet of Things (IoT) applications. The bucket MongoDB design pattern reduces the number of documentation while collecting them, improves index performance, and simplifies data access by leveraging pre-aggregation.
MongoDB 设计中的桶模式有助于管理流式数据,如时间序列、实时分析或物联网(IoT)应用。桶 MongoDB 设计模式在收集数据的同时减少文档数量,提高索引性能,并通过利用预聚合简化数据访问。
For example: Collecting weather data using multiple sensors. This data collection helps to reduce efforts to review temperature and wind speed every minute. However, you would receive a summary per hour, such as:
例如:使用多个传感器收集天气数据。这种数据收集有助于减少每分钟审查温度和风速的工作量。然而,您将每小时收到一份摘要,例如:
- Average temperature 平均温度
- Average wind speed 平均风速
However, you can review the weather forecast in detail. For example, find the temperature per minute and calculate the median and variance. The benefit of the bucket design pattern is that it doesn't repeat device identifiers and entry data with every record. However, if this didn't happen, there would be 1440 records per device per hour, creating a large amount of data.
然而,您可以详细查看天气预报。例如,找到每分钟的温度并计算中位数和方差。桶式设计模式的好处在于它不会在每个记录中重复设备标识符和入口数据。然而,如果这种情况没有发生,每个设备每小时就会有 1440 条记录,从而产生大量数据。
Summary 摘要
In this reading, you've learned that MongoDB design patterns optimize the data model to access the application's patterns. Such as:
在本阅读中,您了解到 MongoDB 设计模式优化数据模型以访问应用程序的模式。例如:
- Approximation 近似
- Attribute 属性
- Polymorphic 多态性
- Outliers 异常值
- Bucket 桶