Why NoSQL?
What are NoSQL databases?
CAP theorem.
BASE approach.
Types of NoSQL databases.
MongoDB features.
MongoDB installation on Windows.
MongoDB installation on Linux.
MongoDB Compass installation on Windows.
Terms used in MongoDB.
Data types in MongoDB.
Database commands.
The Need for NoSQL Databases
Modern application storage capacity exceeds the storage capabilities of the traditional relational database management system (RDBMS).
Modern applications require unknown level of scalability.
Modern applications should be available 24/7.
Data needs to be distributed globally.
Users should be able to read and write data anywhere.
Users always seek reduction of software and hardware costs.
All these challenges have given birth to the NoSQL databases.
What Are NoSQL Databases?
NoSQL databases are open source, nonrelational, distributed databases that allow organizations to analyze huge volumes of data.
Availability.
Fault tolerance.
Scalability.
They do not use SQL as a query language.
Most NoSQL databases are designed to run on clusters.
They operate without a schema, freely adding fields to the database without having to define any changes in structure first.
They are polygot persistent, meaning there are different ways to store the data based on the requirements.
They are designed in such a way that they can be scaled out.
CAP Theorem
Consistency implies that every read fetches the last write.
Availability implies that reads and writes always succeed. In other words, each nonfailing node will return a response in a reasonable amount of time.
Partition tolerance implies that the system will continue to function even when there is a data loss or system failure.
- 1.
Consistency and availability.
- 2.
Consistency and partition tolerance.
- 3.
Availability and partition tolerance.
Note
The partition tolerance property is a must for NoSQL databases.
BASE Approach
Basic availability: The database should be available most of the time.
Soft state: Tempory inconsistency is allowed.
Eventual consistency: The system will come to a consistent state after a certain period.
Types of NoSQL Databases
Types of NoSQL Databases
Data Model | Example | Description |
---|---|---|
Key/Value Store | Dynamo DB, Riak | • Least complex NoSQL options. • Key and a value. |
Column Store | HBase, Big Table | • Also known as wide column store. • Storing data tables as sections of columns of data. |
Document Store | MongoDB, CouchDB | • Extends key/value idea. • Storing data as a document. • Complex NoSQL options. • Each document contains a unique key that is used to retrieve the document. |
Graph Database | Neo4j | • Based on graph theory. • Storing data as nodes, edges, and properties. |
MongoDB Features
MongoDB is an open source, document-oriented NoSQL database written in C++. MongoDB provides high availability, automatic scaling, and high performance. The following sections introduce the features of MongoDB.
Document Database
In many programming languages, a document corresponds to native data types.
Embedded documents help in reducing expensive joins.
MongoDB Is Schemaless
Here, the collection Person has two documents, each with different fields, contents, and size.
MongoDB Uses BSON
Rich Query Language
We discuss MongoDB query language in detail in Chapter 2.
Aggregation Framework
MongoDB provides an aggregation framework to perform aggregation operations. The aggregation framework can group data from multiple documents and perform variety operations on that grouped data. MongoDB provides an aggregation pipeline, the map-reduce function, and single-purpose aggregation methods to perform aggregation operations. We discuss the aggregation framework in detail in Chapter 3.
Indexing
Indexes improve the performance of query execution. MongoDB uses indexes to limit the number of documents scanned. We discuss various indexes in Chapter 4.
GridFS
GridFS is a specification for storing and retrieving large files. GridFS can be used to store files that exceed the BSON maximum document size of 16 MB. GridFS divides the file into parts known as chunks and stores them as separate documents. GridFS divides the file into chunks of 255 KB, except the last chunk, the size of which is based on the file size.
Replication
Replication is a process of copying an instance of a database to a different database server to provide redundancy and high availability. Replication provides fault tolerance against the loss of a single database server. In MongoDB, the replica set is a group of mongod processes that maintain the same data set. We discuss how to create replica sets in Chapter 5.
Sharding
A single server can be a challenge when we need to work with large data sets and high throughput applications in terms of central processing unit (CPU) and inout/output (I/O) capacity. MongoDB uses sharding to support large data sets and high-throughput operations. Sharding is a method for distributing data across multiple systems, discussed in detail in Chapter 5.
The mongo Shell
The mongo shell is an interactive JavaScript interface to MongoDB. The mongo shell is used to query, update data, and perform administrative operations. The mongo shell is a component of MongoDB distributions. When you start the mongod process, the mongo shell will be connected to a MongoDB instance.
Terms Used in MongoDB
MongoDB Terms
Terms | MongoDB |
---|---|
MongoDB server | mongod |
MongoDB client | mongo |
Equivalent Terms for RDBMS and MongoDB
RDBMS | MongoDB |
---|---|
Database | Database |
Table | Collection |
Record | Document |
Columns | Fields or key/value pairs |
ACID transactions | ACID transactions |
Secondary index | Secondary index |
JOINs | Embedded document, $lookup |
GROUP_BY | Aggregation pipeline |
Data Types in MongoDB
MongoDB Data Types
String | Strings are UTF-8 |
---|---|
Integer | Can be 32-bit or 64-bit |
Double | To store floating-point values |
Arrays | To store a list of values into one key |
Timestamps | For MongoDB internal use; values are 64-bit Record when a document has been modified or added |
Date | A 64-bit integer that represents the number of milliseconds since the Unix epoch (January 1, 1970) |
ObjectId | Small, unique, fast to generate, and ordered Consist of 12 bytes, where the first four bytes are a timestamp that reflects the ObjectId’s creation |
Binary data | To store binary data (images, binaries, etc.) |
Null | To store NULL value |
Note
Refer to the following link for data types in MongoDB: https://docs.mongodb.com/manual/reference/bson-types/
MongoDB Installation
Let us learn how to install MongoDB.
Recipe 1-1. Install MongoDB on Windows
In this recipe, we are going to discuss how to install MongoDB on Windows.
Problem
You want to install MongoDB on Windows.
Solution
Download the MongoDB msi installer from https://www.mongodb.com/download-center#enterprise.
How It Works
Let’s follow the steps in this section to install MongoDB on Windows.
Step 1: Install the msi Installer
Right-click the Windows installer, select Run as Administrator, and follow the instructions to install MongoDB.
Step 2: Create a Data Directory
Create datadb directory in C: to store MongoDB data. Directory looks like “C:datadb”.
Step 3: Start the MongoDB Server
You should get a message that states “Waiting for connection on port 27017.”
Step 4: Start the MongoDB Client
We can see the mongo shell.
This confirms, we have installed MongoDB on windows.
Recipe 1-2. Install MongoDB on Ubuntu
In this recipe, we are going to discuss how to install MongoDB on Ubuntu.
Problem
You want to install MongoDB on Ubuntu.
Solution
Download the MongoDB tarball from https://www.mongodb.com/download-center/community.
How It Works
Let’s follow the steps in this section to install MongoDB on Ubuntu.
Step 1: Extract the tarball
Step 2: Create a Data Directory
Create a /data/db directory as shown in Recipe 1-1.
Step 3: Start the MongoDB Server
Step 4: Start the MongoDB Client
Recipe 1-3. Install MongoDB Compass on Windows
In this recipe, we are going to discuss how to install MongoDB Compass on Windows. MongoDB Compass is a simple-to-use graphical user interface (GUI) for interacting with MongoDB.
Problem
You want to install MongoDB Compass on Windows.
Solution
Download the MongoDB Compass msi installer from https://www.mongodb.com/download-center#compass.
How It Works
Let’s follow the steps in this section to install MongoDB Compass on Windows.
Step 1: Install the MongoDB Compass msi Installer
Step 2: Start the MongoDB Server
You should see a message that states “Waiting for connection on port 27017.”
Note
The default port number for MongoDB is 27017.
Step 3: Connect MongoDB Compass with MongoDB Server
We have now connected MongoDB Compass to the MongoDB server.
Working with Database Commands
Next, we discuss database commands in MongoDB.
Recipe 1-4. Create Database
In this recipe, we are going to discuss how to create a database in MongoDB.
Problem
You want to create a database in MongoDB.
Solution
How It Works
Let’s follow the steps in this section to create a database in MongoDB.
Step 1: Create a database
This shows that you are working in the mydb database, so you know we have created a database by that name.
Recipe 1-5. Drop Database
In this recipe, we are going to discuss how to drop a database in MongoDB.
Problem
You want to drop a database in MongoDB.
Solution
How It Works
Let’s follow the steps in this section to drop a database in MongoDB.
Step 1: Drop a Database
We have thus dropped the database named mydb.
Note
If no database is selected, the default database test is dropped.
Recipe 1-6. Display List of Databases
In this recipe, we are going to discuss how to display a list of databases.
Problem
You want to display a list of databases.
Solution
How It Works
Let’s follow the steps in this section to display a list of databases.
Step 1: Display a List of Databases
We can now see the list of databases.
Note
The newly created database mydb is not shown in the list. This is because the database needs to have at least one collection to display in the list. The default database is test.
Recipe 1-7. Display the Version of MongoDB
In this recipe, we are going to discuss how to display the version of MongoDB.
Problem
You want to display the version of MongoDB.
Solution
How It Works
Let’s follow the steps in this section to display the version of MongoDB.
Step 1: Display the Version of MongoDB
We can see that the version of MongoDB is 4.0.2.
Recipe 1-8. Display a List of Commands
In this recipe, we are going to see how to display the list of MongoDB commands.
Problem
You want to display the list of MongoDB commands.
Solution
How It Works
Let’s follow the steps in this section to display a list of commands.
Step 1: Display a List of Commands
We can now see the list of MongoDB commands.
Note
A few scenarios where we can apply MongoDB are e-commerce product catalogs, blogs, and content management.
In next chapter, we discuss how to perform CRUD operations using MongoDB query language.