See Pure’s vision for the future—on demand. Watch now

What Is MongoDB?

What Is MongoDB?

MongoDB is an open source NoSQL database. As a non-relational database, it can process structured, semi-structured, and unstructured data. It uses a non-relational, document-oriented data model and a non-structured query language.

MongoDB is highly flexible and enables you to combine and store multiple types of data. It also stores and handles larger amounts of data than traditional relational databases. MongoDB uses a document storage format called BSON, which is a binary form of JSON (JavaScript Object Notation) that can accommodate more data types.

How Does MongoDB Work?

MongoDB stores data objects in collections and documents instead of the tables and rows used in traditional relational databases. Collections comprise sets of documents, which are equivalent to tables in a relational database. Documents consist of key-value pairs, which are the basic unit of data in MongoDB.

The structure of a document can be changed by simply adding new fields or deleting existing ones. Documents can define a primary key as a unique identifier, and values can be a variety of data types, including other documents, arrays, and arrays of documents.

How Does MongoDB Text Search Work?

A key feature of MongoDB is the text search, which can query string fields for specific text or words. A text search can be performed using a text index or the $text operator.

A text index can either be a string or an array of string elements. To perform a text search query, the collection must contain a text index. A collection can only have one text index, and a single text index can be applied to multiple fields.

A search can also be performed on a collection with a text index using the $text operator. The $text operator tokenizes each search string with white space and treats all punctuation except for “–” and “\” as delimiters. After the search string is tokenized, the operator performs the logical OR operation on the tokens.

Three MongoDB Query Examples

MongoDB uses MongoDB Query Language (MQL) to retrieve data from the database. It’s easy to use and works in a way that’s similar to SQL with CRUD operations for creating, reading, updating, and deleting documents. Function names follow the syntax:

        <database>.<collection_name>. <operation>.

The following are three examples that demonstrate this in practice:

INSERT: Create or insert a new document into a collection. If the collection does not exist, a new collection will be created.

        db.collection.insertOne() inserts one document into a collection.

        db.collection.insertMany() inserts multiple documents into a collection at once.

Here’s what inserting one document into the customer collection looks like:

        db.customer.insertOne (

        {

        firstname: “Jane”,

        lastname: “Mason”

        Address: “232 Petunia Drive, Atlanta, GA, 30311”

        }

FIND: This queries a collection of documents. Query filters and criteria can be applied to find specific documents.

        db.collection.find()

The following code finds all the documents in the customer collection:

        db.customer.find()

UPDATE: This modifies existing documents in a collection.

        db.collection.updateOne()

        db.collection.updateMany()

        db.collection.replaceOne()

Here’s how you’d update one document in the customer collection:

        db.customer.updateOne(

        { firstname: “Jane” },

                       {

                                $set: { “address”, “5 Lavender Ave, Atlanta, GA, 30311”}

                        }

        )

Top Three Advantages of MongoDB

Flexibility

MongoDB has a dynamic schema architecture that works with non-structured data and storage. Because data is stored in flexible, JSON-like documents, the database schema doesn’t have to be predefined and schemas can be modified dynamically without causing downtime.

With MongoDB’s BSON data format, objects in one collection can have different sets of fields, and almost any type of data structure can be modeled and manipulated. For this reason, MongoDB’s flexible database model is especially beneficial as business and data requirements change.

Sharding

MongoDB offers horizontal scaling through a process called sharding. Sharding divides data from a large data set and distributes it across multiple servers. If one server can’t handle a large load of data, it can be automatically divided and distributed without interrupting data processing.

Greater Performance

MongoDB stores data in RAM for faster data access and greater performance when executing queries. It collects data directly from RAM rather than the hard disk, making data reads and writes faster. MongoDB’s non-relational data structure also means that it requires less processing power to search and retrieve data than a relational database.

When Should You Use MongoDB?

Real-Time Analytics

As a NoSQL database, MongoDB is a good choice for integrating and processing big data (i.e., enormous amounts of diverse data too large to be processed by traditional relational databases).

Because MongoDB is schemaless, various data types can be stored and accessed on the fly. MongoDB’s built-in support for sharding also allows it to scale data horizontally across multiple servers. In addition, it provides the flexibility needed to merge hundreds of data sources into a single view for real-time analytics and data integration.

Content Management

MongoDB’s non-structured document model makes it an excellent option for content management and delivery of e-commerce websites, online publications, and web content management systems. Its flexible data model makes it easy to store several types of content, including images, text, and video, as well as metadata.

All related content is stored in a single document, making it easy to add new features and attributes. MongoDB can also be used to store user-generated content like comments, which can be analyzed and used to guide the development of future content.

Four MongoDB Questions Answered

What Is MongoDB vs. MySQL?

MySQL is a relational database management system (RDBMS) maintained by Oracle. It uses a structured query language (SQL), which represents data in predefined tables and rows. MySQL requires the JOIN operator to retrieve data from related tables. MySQL doesn’t allow for effective replication or sharding.

MongoDB is an open source cross-platform database maintained by MongoDB, Inc. It’s a document-based database that aims to handle the data demands of modern software applications. MongoDB uses JavaScript as the query language and represents data as JSON documents. It doesn’t require a predefined schema, which means that documents in the same collection can have different structures.

Is MongoDB Faster than MySQL?

MySQL uses JOIN operations to access and query related data across multiple tables. While this minimizes data duplication, it results in millions of reads and writes that can affect performance.

MongoDB’s document model stores related data together, allowing it to retrieve documents faster than MySQL. Using slave and master replication, MongoDB can process large amounts of unstructured data much faster than MySQL.

When Should You Use MongoDB Instead of MySQL?

Whether you choose MongoDB or MySQL will depend on your specific use cases and business needs, but MongoDB offers some advantages over MySQL. These include:

  • Document-oriented design: Because MongoDB is a NoSQL database, it stores data as documents instead of in a relational format. This makes it more flexible and adaptable to real-world business scenarios. In contrast, MySQL’s relational data schema is predefined, making it rigid and inflexible.
  • Load balancing: MongoDB supports load balancing through sharding, which enables data to scale horizontally. Sharding can split data across multiple MongoDB instances and multiple servers to balance the load in the event of a hardware failure or to enhance performance.
  • Ad hoc queries: MongoDB supports ad hoc queries with searches by field, range queries, and regular expressions. Queries can return specific fields within a document.

Which Database Is Best: MySQL or MongoDB?

MongoDB and MySQL are inherently different database management systems, but both excel in specific scenarios.

MongoDB allows organizations to build applications faster and is suitable for handling various types and large amounts of data. Use MongoDB when you need high data availability, schema flexibility, the ability to scale out quickly, or the ability to support big data and analytical needs.

MySQL is a better choice if your data schema is stable and you don’t need to store unstructured data. Use MySQL if you require a high transaction rate, the security that comes with referential integrity, or if you have a data structure that isn’t likely to change.

Accelerate Open Source Database Workloads with Pure

With automatic failover and horizontal scaling, MongoDB is an open source database built for modern applications. Its document data model supports JSON and maps naturally to object-oriented languages, simplifying development. Its query language is easy for developers to learn and use.

Modernize your storage with Pure Storage® FlashBlade®, the industry's most advanced all-flash storage solution for consolidating fast file and object data. FlashBlade offers:

  • Agile scale-out architecture: FlashBlade handles tens of billions of files and objects with maximum performance and rich data services.
  • Simplified workload consolidation: Deploy, update, and manage FlashBlade with Pure1®.
  • All-flash performance: Gain massive throughput and parallelism with consistent multidimensional performance through FlashBlade fast file and object storage.
800-379-7873 +44 20 3870 2633 +43 720882474 +32 (0) 7 84 80 560 +33 9 75 18 86 78 +49 89 12089 253 +353 1 485 4307 +39 02 9475 9422 +31 (0) 20 201 49 65 +46-101 38 93 22 +45 2856 6610 +47 2195 4481 +351 210 006 108 +966112118066 +27 87551 7857 +34 51 889 8963 +41 31 52 80 624 +90 850 390 21 64 +971 4 5513176 +7 916 716 7308 +65 3158 0960 +603 2298 7123 +66 (0) 2624 0641 +84 43267 3630 +62 21235 84628 +852 3750 7835 +82 2 6001-3330 +886 2 8729 2111 +61 1800 983 289 +64 21 536 736 +55 11 2655-7370 +52 55 9171-1375 +56 2 2368-4581 +57 1 383-2387
Your Browser Is No Longer Supported!

Older browsers often represent security risks. In order to deliver the best possible experience when using our site, please update to any of these latest browsers.