Beginners Guide to MongoDB - spring-boot-in-practice/repo GitHub Wiki
Overview
MongoDB is an open-source, document-based NoSQL database. It stores data in a JSON-like format with enterprise-grade features such as high availability, scalability, and security. The following are a few notable features of the MongoDB database:
- It is an open-source, cross-platform, NoSQL, document-based database
- There is no concept of database schema like relational databases. Data is stored as collections and documents
- There are no complex joins between collections which are usually done in relational database tables. A join is generally performed while storing the data by combining the documents
- Data is stored in BSON format (Binary encoding of JSON-like documents)
- Data stored in collections need not have a similar structure. One document can have a certain set of fields, whereas another document can have a completely different set (both type and amount) of fields
- MongoDB provides a default database named test. If there is no database selected while storing data, MongoDB uses this database to store data
Installing MongoDB
In this section, we will talk about the installation of MongoDB on a local computer. MongoDB is available in two flavors:
- Community Edition
- Enterprise Edition
The Community edition of MongoDB is open-sourced and freely available. Enterprise edition is licensed and is available with an array of enterprise-grade features with commercial support. Step by step instructions on how to download and install the MongoDB community edition can be found here. At the time of installation, MongoDB provides an option to install MongoDB Compass, a GUI console to access MongoDB databases. Alternatively, the MongoDB database can be accessed from the command line shell as well.
MongoDB Components
There are several database components of MongoDB. However, there are two primary components that are of interest to us. We will be using these two components in this article:
- mongod: This is the database daemon that runs in the background
- mongo: MongoDB shell. This is used to connect to the daemon and execute various database commands
Understanding MongoDB Storage Structure
In a relational database, we use a database schema. As described in detail here, a database schema can be defined as below:
A database schema is a collection of database metadata that describes the relationships between objects and information in a database. An easy way to envision a schema is to think of it as a box that holds tables, stored procedures, views, and related data assets. A schema defines the infrastructure of this box
However, there is no concept of a schema in MongoDB. It primarily uses the concept of database, collection, and documents. The following is a detailed description of each of these. For a better understanding, we will define these terms in comparison with Structured Query Language (SQL).
- Database: A database is a physical container for collections.
- Collection: A collection is a group of MongoDB documents. In SQL terminology, this is similar to a database table.
- Document: A document is a set of key-value pairs. In SQL terminology, this is similar to a row in a database table.
- Field: A field is key in a document. In SQL terminology, this is similar to a column in a database table.
- Embedded Document: An embedded document is the joining of multiple documents. In SQL terminology, this is similar to joining several database tables
Read More
Read how to use MongoDB here