Table of Contents
技巧一、使用正确的类型
技巧二、用简单唯一的id替代_id
技巧三、不要用文档做_id
技巧四、不要用数据库引用
技巧五、不要用GridFS处理小的二进制数据
技巧六、处理“无缝”故障切换
技巧七、处理复制组失效以及故障恢复
Home Database Mysql Tutorial 【MongoDB】MongoDB之七大实现技巧

【MongoDB】MongoDB之七大实现技巧

Jun 07, 2016 pm 04:10 PM
mongodb use accomplish Skill correct type

技巧一、使用正确的类型 用正确的类型存放数据大有裨益。数据类型影响数据的查询方式、数据存放顺序和以及占用多少空间。 数字:做为数字使用的字段就用数字存储。也就是做计算或按照大小排序的字段数据库会自动转换溢出的(比如由于$inc操作导致的溢出) 32

技巧一、使用正确的类型

用正确的类型存放数据大有裨益。数据类型影响数据的查询方式、数据存放顺序和以及占用多少空间。
数字:做为数字使用的字段就用数字存储。也就是做计算或按照大小排序的字段 数据库会自动转换溢出的(比如由于$inc操作导致的溢出) 32位浮点数,将其变为64位整数。
日期:一班的用yyy-mm-dd形式的字符串就可以,如果date类型就会总会匹配到毫秒级别
objectId就要作为objectId存储,千万不要存成字符串。这点非常重要,原因是方便查询 二是objectd中含有用的信息,绝大多数驱动都是从有方法中获得文档的创建日期。 第三objectId表示要多占两陪的磁盘空间。

技巧二、用简单唯一的id替代_id

要是数据库没用唯一的字段,那么就用默认的objetId来做_id。但是若是数据本身就有唯一的字段,并且不需要objectId的功能,那么就用自己唯一的之覆盖默认的id就好了。

技巧三、不要用文档做_id

除了不可避免的情况(mapreduce的输出),通常都不应该将文档作为_id。问题在于索引一个文档中的字段和索引文档完全不一样。如果没有每次查询每个子文档的计划,最后会有多个索引。

技巧四、不要用数据库引用

数据库引用一般形式是:{$id:主键,$ref:collectionName}(也可以有可选的$db字段,表示数据库)。若被引用的文档集合是确定的,不妨只用_id引用,这样就同时比用Id和集合名要节省空间。 如果知道要引用的集合,数据库引用就显得浪费空间。

技巧五、不要用GridFS处理小的二进制数据

GridFS需要查询两次,一次获取文件的元信息,另一次获取内容,所以如果用GridFS存储小文件,会使应用查询次数加倍。从根本上说,GridFS是用来将大的二进制对象切成小片存在数据库中。

技巧六、处理“无缝”故障切换

假设发送给服务器一个请求,得到了网络错误,这时驱动有很多选择。若驱动知道有另一台服务器是否自动对其发送请求?要是驱动发送原请求后发生网络故障,或者在服务端响应时候发送故障怎么办?数据库可能已经处理了请求,就不必再次发送。

技巧七、处理复制组失效以及故障恢复

可能出现的情况: 应用抛出异常“not master”异常可能原因:1 . 复制组正在进行故障恢复 2, 网络状况不好,无法链接诶主节点。3.应用链接节点有问题,无法降级为只读节点。





Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is Bitget Launchpool? How to use Bitget Launchpool? What is Bitget Launchpool? How to use Bitget Launchpool? Jun 07, 2024 pm 12:06 PM

BitgetLaunchpool is a dynamic platform designed for all cryptocurrency enthusiasts. BitgetLaunchpool stands out with its unique offering. Here, you can stake your tokens to unlock more rewards, including airdrops, high returns, and a generous prize pool exclusive to early participants. What is BitgetLaunchpool? BitgetLaunchpool is a cryptocurrency platform where tokens can be staked and earned with user-friendly terms and conditions. By investing BGB or other tokens in Launchpool, users have the opportunity to receive free airdrops, earnings and participate in generous bonus pools. The income from pledged assets is calculated within T+1 hours, and the rewards are based on

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

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

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

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

Soland's feeding-level hair-raising airdrop tutorial: 0 cost, no routines, no counter-playing! Soland's feeding-level hair-raising airdrop tutorial: 0 cost, no routines, no counter-playing! Jun 25, 2024 pm 07:13 PM

Soland's feeding-level lubbing tutorial: 0 cost, no routines, no feedback! Recently, hair removal has once again become a topic of discussion among many friends in the currency circle. Of course, this time everyone is generally disappointed or even angry. The reason is for no other reason. First, there was the tug-of-war of Layerzero’s witch screening batch screening, and then the PUA king ZKsync’s Airdrops have caused many people to complain that they were severely criticized, and some even spent tens of thousands of dollars in gas and interaction costs, and the final benefit was less than one-tenth, not counting the time spent and other costs. The two "King of Heaven" level projects have made many people want to quit the terry circle. In fact, we think it is not necessary here, because there are still many opportunities in the encrypted track. Regardless of the scale, project parties have a need to promote it.

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

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).

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

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:

MongoDB and relational database: a comprehensive comparison MongoDB and relational database: a comprehensive comparison Apr 08, 2025 pm 06:30 PM

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

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

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.

See all articles