【Monogdb】MongoDB之十大应用设计技巧
技巧一、速度和完整性的折中 在多个文档中使用的数据可以采用内嵌(反范式话)的方式,也可以采用引用(范式化)的方式。这种策略并没有优劣之分,各自都有优缺点。关键是要选择适合自己的应用场景方案。 反范式化会产生不一致的数据。但要是范式化,应用则
技巧一、速度和完整性的折中
在多个文档中使用的数据可以采用内嵌(反范式话)的方式,也可以采用引用(范式化)的方式。这种策略并没有优劣之分,各自都有优缺点。关键是要选择适合自己的应用场景方案。 反范式化会产生不一致的数据。但要是范式化,应用则必须在每次确认时做额外一次查找。因为极高的性能和瞬间一致性不可兼得,所以必须要想清楚哪个才是应用最需要的。
考虑因素一般包括:
是否总要额外读取一次几乎不怎么改变的数据?
一致性很重要吗?
要不要快速读取?
技巧二、适应未来的数据要范式化
范式化可使数据可用性更加长久,未来可以在不同的应用中以不同的方式查询范式化的数据。这里的前提是有些数据将会一年不断地被各种应用得到。技巧三、尽量单个查询获得数据
mongodb的数据库设计应该从应用单元的查询出发。 应用单元,对于web应用或者移动应用可以将对后端的一次请求视作一个应用单元 对于桌面应用,一次用户交互可以算是一个应用单元 对于分析系统,一个图表的加载算作是一个应用单元技巧四、嵌入关联数据
当在嵌入和引用文档之间犹豫不决时候,不防想想查询的目的是为了获得字段本身还是为了进一步获得更加广泛的信息。如果前者建议采用嵌入关联数据。技巧五、嵌入时间点数据
比如某人更新了个人信息,那么就不需要更改其以往的订单内容技巧六、不要嵌入不断增加的数据
mongodb存储数据的机制决定了对数据的不断追加数据是很低效的。在正常使用中数据和对象的大小应该相对固定。技巧七、预填充数据
如果已经知道未来要用到哪些字段,第一次插入是就带着这些字段会比用到时再创建效率更高。 比如每天都要使用新的集合,最好提前创建。技巧八、尽可能预先分配空间
只要知道文档开始比较小,后来会变得确定的大小,就可以使用这种优化方法。 一开始插入文档的时候就用和最终数据大小一样的垃圾数据填充。即添加一个garbage字段(其中包含一个字符串,串大小与文档最终大小相同)技巧九、用数组存放要匿名访问的内嵌数据
一个常见的问题就是内嵌信息到底是欧也妮个数据还是用文档来存。如果确切知道查询内容就用子文档存。如果有时不太清楚查询的具体内容,则要数组。 当知道一些条目的查询条件时候通常该使用数组。技巧十:文档要自给自足
mongodb是一个无脑的大型数据存储。mongodb几乎不做任何数据处理,仅仅存储数据。要尽量遵守这点,避免让mongodb做些能在客户端完成的计算。即便是写小任务,像求平均值或求和,也要放在客户端去做。
如果要找的信息必须经过计算,且无法直接从文档中获得,有两种定义:
付出高昂的性能代价;优化文档结构,使得这些信息能够从文档中直接获得。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Xiaomi's Redmi brand is gearing up to add another budget phone to its portfolio - the Redmi 14C. The device is confirmed to be released in Vietnam on August 31st. However, ahead of the launch, the phone's specifications have been revealed via a Vietnamese retailer. Redmi14CR Redmi often brings new designs in new series, and Redmi14C is no exception. The phone has a large circular camera module on the back, which is completely different from the design of its predecessor. The blue color version even uses a gradient design to make it look more high-end. However, Redmi14C is actually an economical mobile phone. The camera module consists of four rings; one houses the main 50-megapixel sensor, and another may house the camera for depth information.

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

The best currency trading platforms in China are: Binance: a wide range of token selection, low transaction fees, cutting-edge engines; OKX: Leading derivatives exchange, high liquidity, leverage trading; Gate.io: Over 1,400 transaction pairs, low rates; Bitget: Innovative platform, Copy Trade features, customization strategies.

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports
