Home Database Mysql Tutorial MongoDB简单调研

MongoDB简单调研

Jun 07, 2016 pm 05:31 PM
mongodb oracle database

下面我以mongodb为例,展现一些他的特性和场景,期待NoSQL在当下能被更多的开发者拿来一显身手。mongodb的索引同样也是B树,在一

背景

一直受传统RDB的影响,对于数据库表的设计可能大多数开发者都形成了思维定势。在云计算和大数据背景下,RDBMS正在接近极限,KV存储将受到越来越多的关注。学习NoSQL,不求能革RDBMS的命,但希望在设计思路上能得到一些拓宽,很多场景里,SQL表的设计和计算语句其实蛮难受的。

RDBMS天生不是分布式的,因其保持着ACID的特性发展至今,非常重视数据完整性,,但在机器规模增长的情况下,ACID是不可扩展的。同时,随着数据量和访问频率增加,ACID所要维护的开销在增大。分割数据库,无论水平还是垂直,都是在分散总数据和读取需求,达到优化目的,维护代价和难度也随之上升。而KV的查找本质是散列表,且数据量无论如何增大,查找时间几乎固定不变,即非常适合大规模数据。ACID很注重CAP中的C,而参考现实世界中很多事务,比如快递,从你下单、付款到取货,资金和物品的流转并不严格一致,只要在一段时间内整个交易的最后结果满足一致性就可以了。同样,NoSQL和RDBMS比,更偏向于BASE(Basically Available, Soft-state, Eventually consistent)的折中,重视可用性,但不追求状态的严密性,且满足最终一致性。下面我以mongodb为例,展现一些他的特性和场景,期待NoSQL在当下能被更多的开发者拿来一显身手。

mongodb与RDBMS

mongodb是面向文档的nosql,CouchDB则是这一类数据库的元祖。从总体上看,

mongodb是最亲和RDBMS的一个NoSQL,能解决大部分关系型数据库解决的问题

跟面向列存储的HBase相比,面向文档存储和面向行存储更接近,比如在没有索引的情况下,扫描整个表内记录,同样是扫描全文档,及文档的每个字段

mongodb的索引同样也是B树,在一些索引的优化和设计上会和MySQL比较相似(当然需要遵循mongo的设计来做,不完全划等号)

你可以把mongodb拿RDBMS一样来使用(当然不推荐这么做),无非是将一行记录变成mongodb里的json对,在document(相当于mysql的table)之间,也可以做类似外键一样的引用

mongodb虽然没有严格的事务性操作,但是开发者自己可以做到类似事务的效果。这一点也算是mongodb贴近RDBMS的一个表现吧。

以下会从各个主要关注点来展开mongo的特性,展现角度更偏向于想要调研使用mongodb的人,看看mongodb是否符合自己的业务场景,也希望我的分析会有所帮助。

存储结构怎么样

Mongodb的存储类似JSON,每个db内有多个collection,相当于table,每个collection内是许许多多的document,这个document的schemeless的。本质上,他的面向文档指的是key-value中的value,而这个value可以是一个值(引用id或基本类型),可以是一个数组,也可以是一个文档(嵌套的json对)。

一对多是最常遇到的场景,mysql中要使用两张或以上表的关联甚至join进行查询,在mongo中直接使用嵌套型或引用型(用id)就可以了。没有特殊需求的话,嵌套的方式只要一张"表"就可以实现。比如我这样建立一个人的信息:

{
    id : 1,
    name : "pelick",
    hobbies : {
        "GameA", "GameB", "GameC"
    },
    friends : {
        male : {
              2, 3, 4 # id refer to other person
        },
        female : {
            {
                  name : "Rita",
                  hobbies : { "dancing" }
            },
            {
                  name : "Kaka",
                  nickname : "Riva"
            }
          }
      }
}

上述这样的结构中,展现了无模式、value为数组、嵌套、引用等。

处理好多对多的关系可谓是NoSQL的精髓所在。理论上,可以在一个集合中完成存储。不过实际上这样的情况非常罕见。这是由于查询的多样性所导致的,若是只有一种类型的查询,则这种多对多的关系放在一个良好设计的集合中,虽然会有大量的冗余,但是效率一定是最高的。如何设计这种数据库的关键就是看你有多少种查询,每一种的频率是多少,使用的其他要求是什么样的。对于不同的查询,同样的数据库设计的性能也是大不一样。还有一点,一般不要拆成三个集合,这是传统的关系型数据库的思维方式。而常见的情况就是拆成两个集合,然后有一部分冗余,对最常用的查询做一个索引。

总结就是两张表,一张里面存了另外一张里的id集合,有冗余存放,主要是根据查询场景设计和建索引,不要和RDBMS一样变三张。此外还有个好处是可以进行正反向查询,在各自的字段里加上id数组。

接下来请看第2页精彩内容

推荐阅读:

 

CentOS编译安装MongoDB

 

CentOS 编译安装 MongoDB与mongoDB的php扩展

CentOS 6 使用 yum 安装MongoDB及服务器端配置

Ubuntu 13.04下安装MongoDB2.4.3

如何在MongoDB中建立新数据库和集合

MongoDB入门必读(概念与实战并重)

linux

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
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:

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

How to choose a database for GitLab on CentOS How to choose a database for GitLab on CentOS Apr 14, 2025 pm 04:48 PM

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Real-World MySQL: Examples and Use Cases Real-World MySQL: Examples and Use Cases Apr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

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