MongoDB and MySQL Compared

Overview

The relational database has been the foundation of enterprise applications for decades, and since MySQL’s release in 1995 it has been a popular and inexpensive option. Yet with the explosion in the volume and variety of data in recent years, non-relational database technologies like MongoDB have emerged to address the requirements of new applications. MongoDB is used for new applications as well as to augment or replace existing relational infrastructure.

What is MySQL?

MySQL is a popular open-source relational database management system (RDBMS) that is developed, distributed and supported by Oracle Corporation. Like other relational systems, MySQL stores data in tables and uses structured query language (SQL) for database access. In MySQL, you pre-define your database schema based on your requirements and set up rules to govern the relationships between fields in your tables. In MySQL, related information may be stored in separate tables, but associated through the use of joins. In this way, data duplication is minimized.

What is MongoDB?

MongoDB is an open-source database developed by MongoDB, Inc. MongoDB stores data in JSON-like documents that can vary in structure. Related information is stored together for fast query access through the MongoDB query language. MongoDB uses dynamic schemas, meaning that you can create records without first defining the structure, such as the fields or the types of their values. You can change the structure of records (which we call documents) simply by adding new fields or deleting existing ones. This data model give you the ability to represent hierarchical relationships, to store arrays, and other more complex structures easily. Documents in a collection need not have an identical set of fields and denormalization of data is common. MongoDB was also designed with high availability and scalability in mind, and includes out-of-the-box replication and auto-sharding.

Terminology and Concepts

Many concepts in MySQL have close analogs in MongoDB. This table outlines some of the common concepts in each system.

MySQLMongoDB
TableCollection
RowDocument
ColumnField
JoinsEmbedded documents, linking

Feature Comparison

Like MySQL, MongoDB offers a rich set of features and functionality far beyond those offered in simple key-value stores. MongoDB has a query language, highly-functional secondary indexes (including text search and geospatial), a powerful aggregation framework for data analysis, and more. With MongoDB you can also make use of these features across more diverse data types than with a relational database, and at scale.

MySQLMongoDB
Rich Data ModelNoYes
Dynamic SchemaNoYes
Typed DataYesYes
Data LocalityNoYes
Field UpdatesYesYes
Easy for ProgrammersNoYes
Complex TransactionsYesNo
AuditingYesYes
Auto-ShardingNoYes

Query Language

Both MySQL and MongoDB have a rich query language. A comprehensive list of statements can be found in the MongoDB documentation.

MySQLMongoDB
INSERT INTO users (user_id, age, status)
VALUES ('bcd001', 45, 'A')
db.users.insert({
  user_id: 'bcd001',
  age: 45,
  status: 'A'
})
SELECT * FROM users
db.users.find()
UPDATE users SET status = 'C'
WHERE age > 25
db.users.update(
  { age: { $gt: 25 } },
  { $set: { status: 'C' } },
  { multi: true }
)

Why use MongoDB instead of MySQL?

Organizations of all sizes are adopting MongoDB because it enables them to build applications faster, handle highly diverse data types, and manage applications more efficiently at scale.

Development is simplified as MongoDB documents map naturally to modern, object-oriented programming languages. Using MongoDB removes the complex object-relational mapping (ORM) layer that translates objects in code to relational tables.

MongoDB’s flexible data model also means that your database schema can evolve with business requirements. For example, schema changes that took days of weeks in The Weather Channel's MySQL databases could be made in just hours with MongoDB.

MongoDB can also be scaled within and across multiple distributed data centers, providing new levels of availability and scalability previously unachievable with relational databases like MySQL. As your deployments grow in terms of data volume and throughput, MongoDB scales easily with no downtime, and without changing your application. In contrast, to achieve scale with MySQL often requires significant, custom engineering work. Baidu migrated from MySQL to MongoDB to support its rapidly growing business. The Chinese internet services giant now powers over 100 apps and manages in excess of 1PB of data with its MongoDB cluster.

What are common use cases for MongoDB?

MongoDB is a general purpose database that is used for a variety of use cases. The most common use cases for MongoDB include Single View, Internet of Things, Mobile, Real-Time Analytics, Personalization, Catalog, and Content Management.

When would MySQL be a better fit?

While most modern applications require a flexible, scalable system like MongoDB, there are use cases for which a relational database like MySQL would be better suited. Applications that require complex, multi-row transactions (e.g., a double-entry bookkeeping system) would be good examples. MongoDB is not a drop-in replacement for legacy applications built around the relational data model and SQL.

A concrete example would be the booking engine behind a travel reservation system, which also typically involves complex transactions. While the core booking engine might run on MySQL, those parts of the app that engage with users – serving up content, integrating with social networks, managing sessions – would be better placed in MongoDB

Are MongoDB and MySQL used together?

There are many examples of hybrid deployments of MongoDB and MySQL. In some cases, it’s a matter of using the right tool for the job. For example, many e-commerce applications use a combination of MongoDB and MySQL. The product catalog, which includes multiple products with different attributes, is a good fit for MongoDB’s flexible data model. On the other hand, the checkout system, which requires complex transactions, would likely be built on MySQL or another relational technology.

In other cases, new business requirements push organizations to adopt MongoDB for the next-generation components of their applications. For example, Sage Group, one of the world’s leading suppliers of business management software and services, integrated MongoDB into its popular Enterprise Resource Planning (ERP) solution for midsize companies. Sage customers now enjoy a higher degree of functionality and personalization as a result of the integration. While many Sage products were originally built on and continue to run on MySQL, the latest user experience functionality centers around MongoDB.

These few exceptions aside, we think MongoDB is almost always a better option than MySQL because of its flexible data model and scalable architecture.

Want to Learn More? Get the RDBMS to MongoDB Migration Guide

Relational databases are being pushed beyond their limits because of the way we build and run applications today, coupled with growth in data sources and user loads. To address these challenges, companies like MTV and Cisco have migrated successfully from relational databases to MongoDB. In this white paper, you'll learn:

  • Step by step how to migrate from a relational database to MongoDB.

  • The relevant technical considerations, such as differences between the relational and document data models and the implications for schema design.

  • Indexing, queries, application integration and data migration.