© Subhashini Chellappan and Dharanitharan Ganesan 2020
S. Chellappan, D. GanesanMongoDB Recipeshttps://doi.org/10.1007/978-1-4842-4891-1_1

1. MongoDB Features and Installation

Subhashini Chellappan1  and Dharanitharan Ganesan2
(1)
Bangalore, India
(2)
Krishnagiri, Tamil Nadu, India
 
This chapter describes NoSQL databases, various features of MongoDB, and how to interact with MongoDB. We are going to focus on the following topics:
  • 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

There are several database challenges for modern applications, including the following.
  • 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.

The following are some of the key advantages of NoSQL databases:
  • Availability.

  • Fault tolerance.

  • Scalability.

NoSQL databases have the following characteristics:
  • 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

The CAP theorem states that any distributed system can satisfy only two of these three properties:
  • 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.

Figure 1-1 is a graphical representation of the CAP theorem.
../images/475461_1_En_1_Chapter/475461_1_En_1_Fig1_HTML.jpg
Figure 1-1

CAP theorem

CAP therorem categorizes a system according to three categories:
  1. 1.

    Consistency and availability.

     
  2. 2.

    Consistency and partition tolerance.

     
  3. 3.

    Availability and partition tolerance.

     

Note

The partition tolerance property is a must for NoSQL databases.

BASE Approach

NoSQL databases are based on the BASE approach. BASE stands for:
  • 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

NoSQL databases are generally classified into four types, as shown in Table 1-1.
Table 1-1

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 MongoDB, a record is represented as a document. A document is a combination of field and value pairs. MongoDB documents are similar to JavaScript Object Notation (JSON) documents. Figure 1-2 represents a document.
../images/475461_1_En_1_Chapter/475461_1_En_1_Fig2_HTML.jpg
Figure 1-2

A document

These are the advantages for using documents.
  • In many programming languages, a document corresponds to native data types.

  • Embedded documents help in reducing expensive joins.

MongoDB Is Schemaless

MongoDB is a schemaless database, which means a collection (like a table in an RDBMS) can hold different documents (like records in an RDBMS). This way, MongoDB provides flexibility in dealing with the schemas in a database. Refer to the following collection, Person.
{name:"Aruna M S", age:12 } //document with two fields.
{ssn:100023412, name:"Anbu M S",groups:["sports","news"]} //document with three fields.

Here, the collection Person has two documents, each with different fields, contents, and size.

MongoDB Uses BSON

JSON is an open source standard format for data interchange. Document databases use JSON format to store records. Here is an example of a JSON document.
{
      name:"John",
      addresses:[
                {
                    address:"123,River Road",
                    status:"office"
                },
                {
                    address:"345,Mount Road",
                    status:"personal"
                 }
                 ]
    }
MongoDB represents JSON documents in a binary-encoded format, Binary JSON (BSON), behind the scenes. BSON extends the JSON model to provide additional data types such as dates that are not supported by JSON. BSON uses the _id field called ObjectId as the primary key. The value of the _id field is generated either by the MongoDB service or by an application program. Here is an example of ObjectId:
"_id": ObjectId("12e6789f4b01d67d71da3211")

Rich Query Language

MongoDB provides a rich query language to support create, read, update, and delete (CRUD) operations, data aggregation, and text searches. Let us understand how to query a collection in MongoDB with an example. Consider the following collection, Employee.
{_id: 10001, name:"Subhashini",unit:"Hadoop"}
{_id: 10002, name:"Shobana", unit:"Spark"}
To display employee details, the MongoDB query would be
db.Employee.find();

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

Let us understand the terms used in MongoDB, provided in Table 1-2.
Table 1-2

MongoDB Terms

Terms

MongoDB

MongoDB server

mongod

MongoDB client

mongo

Table 1-3 shows the equivalent terms used in RDBMS and MongoDB.
Table 1-3

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

Table 1-4 provides a description of the data types in MongoDB.
Table 1-4

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

Open the command prompt and navigate to the MongoDB installation folder. Issue the following command to start the MongoDB server, as shown in Figure 1-3.
mongod.exe
../images/475461_1_En_1_Chapter/475461_1_En_1_Fig3_HTML.jpg
Figure 1-3

Starting MongoDB Server

You should get a message that states “Waiting for connection on port 27017.”

Step 4: Start the MongoDB Client

Open another command prompt and navigate to the MongoDB installation folder. Issue the following command to start the MongoDB client, as shown in Figure 1-4.
mongo.exe
../images/475461_1_En_1_Chapter/475461_1_En_1_Fig4_HTML.jpg
Figure 1-4

Starting 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

Issue the following command to untar the tarball:
tar -xzf mongodb-linux-x86_64-ubuntu1604-4.0.8.tgz

Step 2: Create a Data Directory

Create a /data/db directory as shown in Recipe 1-1.

Step 3: Start the MongoDB Server

Open the terminal, navigate to the MongoDB installation folder, and issue the following command to start the MongoDB server.
./mongod --dbpath <data directory path>

Step 4: Start the MongoDB Client

Open another terminal, navigate to the MongoDB installation folder, and issue the following command to start the MongoDB client.
./mongo.exe

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

Right-click the MongoDB Compass Windows installer, select Run as Administrator, and follow the instructions to install MongoDB Compass. Figure 1-5 shows the MongoDB Compass GUI.
../images/475461_1_En_1_Chapter/475461_1_En_1_Fig5_HTML.jpg
Figure 1-5

MongoDB Compass GUI

Step 2: Start the MongoDB Server

Open the command prompt, navigate to the MongoDB installation folder, and issue the following command to start the MongoDB server.
mongod.exe

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

Click the New Connection tab. Enter the required details, as shown in Figure 1-6.
../images/475461_1_En_1_Chapter/475461_1_En_1_Fig6_HTML.jpg
Figure 1-6

New Connection page

Click Connect to connect to the MongoDB Server as shown in Figure 1-7.
../images/475461_1_En_1_Chapter/475461_1_En_1_Fig7_HTML.jpg
Figure 1-7

Connecting MongoDB Compass to the 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

Use the following syntax to create a database in MongoDB.
use <database name>

How It Works

Let’s follow the steps in this section to create a database in MongoDB.

Step 1: Create a database

To create a database named mydb, use this command:
use mydb
Here is the output,
> use mydb
switched to db mydb
>
To confirm the existence of the database, type the following command in the mongo shell.
> db
mydb
>

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

Use the following syntax to drop a database in MongoDB.
db.dropDatabase()

How It Works

Let’s follow the steps in this section to drop a database in MongoDB.

Step 1: Drop a Database

To drop a database, first ensure that you are working in the database that you want to drop. Use the db.dropDatabase() method .
use mydb
Here is the output,
> use mydb
switched to db mydb
>
> db.dropDatabase()
{ "ok" : 1 }
>

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

Use the following syntax to display a list of databases.
show dbs
show databases

How It Works

Let’s follow the steps in this section to display a list of databases.

Step 1: Display a List of Databases

Type the following command in the mongo shell.
show dbs
show databases
Here is the output,
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB
>
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB
>

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

Use the following syntax to display the version of MongoDB.
db.version()

How It Works

Let’s follow the steps in this section to display the version of MongoDB.

Step 1: Display the Version of MongoDB

Type the following command in the mongo shell.
db.version()
Here is the output,
> db.version()
4.0.2
>

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

Use the following syntax to display a list of commands.
db.help()

How It Works

Let’s follow the steps in this section to display a list of commands.

Step 1: Display a List of Commands

Type the following command in the mongo shell.
db.help()
Here is the output,
> db.help()
dB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.164