Intro to MongoDB
In this article, see an introduction to MongoDB and look at the architecture and features.
Join the DZone community and get the full member experience.Join For Free
The word “database” instantly creates a picture of a table having rows and columns. Typically, any system architecture has a relational database management system (RDBMS) to store data. Data in an RDBMS is stored in tables containing rows and columns, and tables are connected to each other via relational models.
Examples of traditional RDBMSs include Oracle, MySQL, Microsoft SQL Server, DB2, etc. These databases have been in the market for a substantial amount of time, as they are highly efficient and deliver great performance.
However, an alternative to the conventional RDBMS that is trending in the market is NoSQL. NoSQL, which means “Not Only SQL”, is a database that stores data in non-tabular forms like document stores, graph DB, BigTable, etc. One of the leading examples of NoSQL is MongoDB.
This article will assist you with an intro to MongoDB, architecture, features, and why has it achieved huge popularity.
You might also want to read: Getting Started With MongoDB
- MongoDB is an open source, cross-platform, document-oriented NoSQL database that stores data in the form of documents and collections. A document is nothing but a record that contains all information about itself. A group of documents is called a collection.
- A document can contain a number of fields (value), regarding the details of the record. One document size can be different from the other as two different documents can store a varied number of fields. Every field (value) has an associated key mapped to it. The field can be of any data type like the general text, number, etc. A field can also be a link to another document or arrays. MongoDB uses BSON (binary encoding form of JSON), to include additional data types like date, that is not compatible with JSON.
- There is no forced relation between the fields, but links and data models can be established while creation. Data models facilitate the storage of arrays, establish hierarchical relations, etc.
- Data is stored in a stream, and not in a schema, unlike the RDBMS.
Image source: educba.com
If you compare it with an RDBMS, documents correspond to the record (row), which has a unique identifier ID (primary key), fields/values are columns that have an associated key to them, and collections are the equivalent of a table.
With MongoDB in the market, more and more developers are using it in specific applications due to its many benefits and advantages.
MongoDB is a specialized database for Big Data processing. It can contain large volumes of data, making it highly scalable. It also supports the concept of horizontal scaling, where data can be distributed across multiple DB instances. This helps in data load balancing as well as duplication of data as a backup.
MongoDB is schema-less, which means it doesn’t enforce relations between fields, rather, it allows the storage of values of any data type in a stream. Since data is stored in a document/collection type of model, it offers flexibility in terms of data modeling and data alteration and updation on the fly without the hassle of schema updation like in an RDBMS.
Sharding is an interesting and very powerful methodology in MongoDB. MongoDB allows the distribution of data onto several servers, as opposed to a single server. For applications having huge volumes of data, the load can be balanced by storing data in several servers. This also helps in increasing the capacity of the database as well as concurrent and parallel processing of tasks.
Data Replication and Recovery
MongoDB provides specialized tools for data replication as a backup, in times of any system failure. The main server, acting as the primary server, stores all the data and takes care of the read/write and other transactions. The secondary server is a replica of the primary server, which can take over and act as a primary server in case the primary server crashes or fails, thereby providing high availability all the time.
High Performance and Speed
MongoDB supports different features like dynamic ad-hoc querying, indexing for faster search functionality, tools like Aggregation pipeline for aggregation queries, etc. that help in faster processing. Querying is also quicker, as compared to an RDBMS, as related data are stored together in documents. This guarantees high performance, even in critical high volume situations.
With MongoDB introduction as a chief NoSQL database, developers are expressing increased interest in it because of its varied features as discussed above. However, there are a couple of drawbacks to MongoDB as well, which doesn’t make it appropriate for all kinds of applications.
In the end, it is the developer’s responsibility to decide which database to be used by studying the project thoroughly and understanding if MongoDB is applicable or not.
Published at DZone with permission of Sajan Agrawal. See the original article here.
Opinions expressed by DZone contributors are their own.