Skip to Content

什麼是 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.

MongoDB 是一個開源 NoSQL 資料庫。作為非關聯式資料庫,它可以處理結構化、半結構化和非結構化資料。它使用非關聯性、文件導向的資料模型,以及非結構化的查詢語言。

MongoDB 具有高度彈性,可讓您合併並儲存多種類型的資料。它也比傳統關聯式資料庫儲存並處理更多資料。MongoDB 使用名為 BSON 的文件儲存格式,這是一種 JSON(JavaScript Object Notation)的二進位形式,可容納更多資料類型。

MongoDB 如何運作?

MongoDB 將資料物件儲存在收集與文件中,而非傳統關聯式資料庫所使用的表格與資料列。集合包含一組文件,等同於關聯式資料庫中的表格。文件包含鍵值組,這是 MongoDB 的基本資料單位。

只要新增欄位或刪除現有欄位,即可變更文件的結構。文件可將主鍵定義為唯一識別碼,數值可以是各種資料類型,包括其他文件、陣列和文件陣列。

MongoDB 文字搜尋如何運作?

MongoDB 的一個關鍵功能是文字搜尋,可以查詢特定文字的字串欄位。可使用文字索引或 $text 運算子執行文字搜尋。

文字索引可以是字串或字串元素陣列。若要執行文字搜尋查詢,集合必須包含文字索引。集合只能有一個文字索引,單一文字索引可以套用至多個欄位。

也可以使用 $text 運算子,對具有文字索引的集合進行搜尋。$text 運算子用空白標記每個搜尋字串,並將所有標點符號視為分隔符號,除了 “–” 和 “\”。搜尋字串被權杖化後,操作人員在權杖上執行邏輯 OR 操作。

三個 MongoDB 查詢範例

MongoDB 使用 MongoDB Query Language(MQL)從資料庫擷取資料。使用起來非常簡單,運作方式與 SQL 和 CRUD 操作類似,可用於建立、讀取、更新和刪除文件。函數名稱遵循語法:

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

以下是三個實際示範的範例:

插入:建立新文件或將文件插入集合。如果集合不存在,將建立新的集合。

        db.collection.insertOne() 會將一份文件插入一個集合中。

        db.collection.insertMany() 一次將多份文件插入一個集合中。

以下是將一份文件插入客戶收藏的樣子:

        db.customer.insertOne(

        {

        名字:「Jane」,

        姓氏:“Mason”

        地址:“232 Petunia Drive, Atlanta, GA, 30311”

        }

尋找:這會查詢文件的集合。查詢篩選條件可用於尋找特定文件。

        db.collection.find()

下列代碼可在客戶集合中找到所有文件:

        db.customer.find()

更新:如此可修改集合中的現有文件。

        db.collection.updateOne()

        db.collection.updateMany()

        db.collection.replaceOne()

以下是您在客戶集合中更新文件的方式:

        db.customer.updateOne(

        {名字:“Jane” },

                       {

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

                        }

        )

MongoDB 的三大優勢

彈性

MongoDB 具有動態架構,適用於非結構化資料與儲存。因為資料是儲存在彈性的 JSON 類文件中,所以資料庫架構不必預先定義,而且可以動態修改架構,不會造成停機時間。

有了 MongoDB 的 BSON 資料格式,一個集合中的物件可以有不同的欄位集,幾乎任何類型的資料結構都可以進行建模和操作。因此,MongoDB 的彈性資料庫模式在業務和資料需求改變時特別有用。

陰影

MongoDB 透過所謂的共享流程提供水平擴展。遮蔽會將資料從大型資料集中分割出來,並分散到多個伺服器中。如果一台伺服器無法處理大量資料,就可自動分割和分配資料,而不會中斷資料處理。

更高的效能

MongoDB 將資料儲存在 RAM 中,以便在執行查詢時加快資料存取速度,並提升效能。它直接從 RAM 而非硬碟收集資料,使資料讀取和寫入更快速。MongoDB 的非關聯式資料結構也意味著,相較於關聯式資料庫,搜尋和擷取資料所需的處理能力較低。

您何時該使用 MongoDB

即時分析

作為 NoSQL 資料庫,MongoDB 是整合和處理大數據(即大量不同的資料太大,無法由傳統關聯式資料庫處理)的好選擇。

由於 MongoDB 是無架構的,因此可以即時儲存和存取各種資料類型。MongoDB 內建的共享支援也讓它能夠在多個伺服器上水平擴展資料。此外,它提供了將數百個資料來源合併為單一視圖所需的彈性,以進行即時分析和資料整合。

內容管理

MongoDB 的非結構化文件模式使其成為內容管理和交付電子商務網站、線上出版物和網路內容管理系統的絕佳選擇。其靈活的資料模式可讓您輕鬆儲存多種內容,包括影像、文字、影片,以及中繼資料。

所有相關內容都儲存在單一文件中,讓您輕鬆新增新功能與屬性。MongoDB 也可用於儲存使用者生成的內容,如評論,這些內容可用於分析和引導未來內容的開發。

回答了四個 MongoDB 問題

什麼是 MongoDB 與 MySQL?

MySQL 是由 Oracle 維護的關聯式資料庫管理系統 (RDBMS)。它使用結構化查詢語言(SQL),代表預先定義的表格和資料列中的資料。MySQL 需要 JOIN 運算子從相關表格擷取資料。MySQL 不允許有效的複寫或共享。

MongoDB 是由 MongoDB, Inc. 維護的開源跨平台資料庫。 它是以文件為基礎的資料庫,旨在處理現代軟體應用程式的資料需求。MongoDB 使用 JavaScript 作為查詢語言,並代表資料作為 JSON 文件。它不需要預先定義的架構,這表示同一集合中的文件可能有不同的結構。

MongoDB 比 MySQL 更快嗎?

MySQL 使用 JOIN 操作來存取和查詢多個表格的相關資料。雖然如此可將重複資料減至最低,但會產生數百萬筆可能影響效能的讀寫資料。

MongoDB 的文件模型將相關資料儲存在一起,讓擷取文件的速度比 MySQL 更快。MongoDB 使用從屬與主複寫功能,能夠比 MySQL 更快處理大量非結構化資料。

您何時該使用 MongoDB 而非 MySQL?

無論您選擇 MongoDB 還是 MySQL,都取決於您的特定使用案例和業務需求,但 MongoDB 比 MySQL 更具有優勢。包括:

  • 文件導向設計: 由於 MongoDB 是 NoSQL 資料庫,因此將資料儲存為文件而非關聯格式。這使得它更加靈活,能夠適應現實世界的業務情況。相較之下,MySQL 的關聯式資料架構是預先定義的,因此相當堅固且不具彈性。
  • 負載平衡: MongoDB 透過共享支援負載平衡,讓資料能夠水平擴展。Sharding 可將資料分割至多個 MongoDB 執行個體和多個伺服器,以便在硬體故障時平衡負載,或提升效能。
  • 特別查詢: MongoDB 支援特別查詢,可依欄位、範圍查詢和規則運算式進行搜尋。查詢可傳回文件中的特定欄位。

哪個資料庫最適合:MySQL 或 MongoDB?

MongoDB 和 MySQL 本質上是不同的資料庫管理系統,但兩者在特定情境下都表現優異。

MongoDB 能讓組織更快速地建立應用程式,並適用於處理各種類型和大量資料。當您需要高資料可用性、架構彈性、快速擴展的能力,或支援大數據和分析需求的能力時,請使用 MongoDB。

如果您的資料架構穩定,而且不需要儲存非結構化資料,MySQL 是更好的選擇。如果您需要高交易率、具有參考完整性的安全性,或是資料結構不可能改變,請使用 MySQL。

使用 Pure 加速開源資料庫工作負載

MongoDB 具備自動故障轉移與水平擴充功能,是為現代化應用程式打造的開源資料庫。其文件資料模型支援 JSON,自然地對應至物件導向語言,簡化開發過程。其查詢語言讓開發人員易於學習和使用。

Pure Storage® FlashBlade® 是業界最先進的全快閃儲存達到現代化。FlashBlade 提供:

  • 靈活的橫向擴充架構:FlashBlade 能處理數十億個檔案和物件,提供最高效能和豐富的資料服務。
  • 簡化工作負載合併: 使用 Pure1® 部署、更新並管理 FlashBlade。
  • 全快閃效能: 透過 FlashBlade 高速檔案和物件式資料儲存,取得龐大的傳輸量和並行處理,並擁有一致的多維效能。
11/2024
How Healthy Is Your Data Platform Really?
Complete this self-guided wellness check to help determine if your data platform can successfully adapt with your organization into the future.
資訊圖表
1 頁面
聯繫我們
問題或建議

如對Pure的產品或認證,有任何的疑問或建議,歡迎與我們聯繫!

預約試用

預約現場示範,親眼看看 Pure 如何幫助您將資料轉化為強大的成果。 

聯絡我們:886-2-3725-7989

媒體:pr@purestorage.com

 

Pure Storage總部

34F, Taipei Nanshan Plaza,

No. 100, Songren Road,

Xinyi District,

Taipei City 110016

Taiwan (R.O.C.)

800-379-7873 (一般資訊)

info@purestorage.com

關閉
您的瀏覽器已不受支援!

較舊版的瀏覽器通常存在安全風險。為讓您使用我們網站時得到最佳體驗,請更新為這些最新瀏覽器其中一個。