Home Data MongoDB 4 Quick Start Guide

MongoDB 4 Quick Start Guide

By Doug Bierer
books-svg-icon Book
eBook $25.99 $17.99
Print $32.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $25.99 $17.99
Print $32.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Introducing MongoDB
About this book
MongoDB has grown to become the de facto NoSQL database with millions of users, from small start-ups to Fortune 500 companies. It can solve problems that are considered difficult, if not impossible, for aging RDBMS technologies. Written for version 4 of MongoDB, this book is the easiest way to get started with MongoDB. You will start by getting a MongoDB installation up and running in a safe and secure manner. You will learn how to perform mission-critical create, read, update, and delete operations, and set up database security. You will also learn about advanced features of MongoDB such as the aggregation pipeline, replication, and sharding. You will learn how to build a simple web application that uses MongoDB to respond to AJAX queries, and see how to make use of the MongoDB programming language driver for PHP. The examples incorporate new features available in MongoDB version 4 where appropriate.
Publication date:
September 2018
Publisher
Packt
Pages
192
ISBN
9781789343533

 

Introducing MongoDB

This chapter gives you a brief overview of MongoDB, including answering such questions as what is MongoDB?, why use it?, and what are its benefits? It then covers installing the MongoDB Community Edition (free version) on a Windows server and on Linux. You will learn the installation differences between RPM based Linux distributions (Red Hat, Fedora, CentOS) and Deb based (Debian, Ubuntu). There is also a brief summary of how to install directly from source.

The topics that we will learn in this chapter are:

  • Overview of MongoDB
  • Installing MongoDB
  • Installing MongoDB on Linux
 

Overview of MongoDB

MongoDB represents a radical and much needed departure from relational database technology. Dr. Edgar F. Codd (https://en.wikipedia.org/wiki/Edgar_F._Codd), an English computer scientist working for IBM, published his seminal paper, A Relational Model of Data for Large Shared Data Banks in 1970. It formed the basis for what we now know as RDBMS (Relational Database Management Systems), using SQL (Structured Query Language), adopted by IBM, Relational Software (later Oracle), and Ingres (https://en.wikipedia.org/wiki/Ingres_(database), a research project at the University of California in Berkeley. Ingres, in turn, spawned Postgres, Sybase, Microsoft SQL Server, and others.

The first version of MongoDB was introduced in 2009 by 10gen (https://en.wikipedia.org/wiki/MongoDB_Inc.#History) (later MongoDB Inc.) to address a crying need not addressed by the current stable of RDBMS systems, which were, for the most part, based on almost 50-year-old technology; handling big data and modeling objects. Initially proprietary, MongoDB was later released as open source.

DB-Engines (https://db-engines.com/en/ranking) provides up-to-date rankings of competing database systems. It is of interest to note that MongoDB is now in the Top 10, currently ranked fifth. However, you should also note that the score assigned to MongoDB is 343.79 compared with the number one ranked system, Oracle, with a score of 1,311.25.

Handling big data

One massive problem faced by legacy RDBMS systems is difficulty managing Big Data (https://en.wikipedia.org/wiki/Big_data). Examples would include data produced by the NASA Center for Climate Change, the Human Genome Project, which analyzes strands of DNA, or the Sloan Digital Sky Survey, which collects astronomical data. RDBMS systems are designed to maximize storage, which was an expensive resource 50 years ago. In the 21st century, storage costs have dropped dramatically, making this a secondary consideration. Another aspect of RDBMS systems is their ability to provide flexibility by way of creating relations between tables, which by its very nature introduces overheads, compounded when handling big data.

MongoDB addresses the needs of big data by incorporating modern algorithms such as map reduce (https://en.wikipedia.org/wiki/MapReduce), which allows for parallel distributed processing on a cluster of servers. In addition, MongoDB has a feature referred to as sharding, which allows fragments of a database to be stored and processed on multiple servers.

It should be noted that although MongoDB is designed to handle big data, it is actually more of a general purpose platform. If your only need is to handle big data, it might be worth your while to investigate Apache Cassandra (https://cassandra.apache.org/) with Hadoop (http://hadoop.apache.org/), which is expressly designed to handle massive amounts of data.

Modeling objects without SQL

A classic paradox in object oriented programming (OOP) code that requires database access is caused by the two-dimensional architecture of the traditional RDBMS. The two dimensions, rows and columns, are in turn grouped into tables, much like a legacy spreadsheet. In order to achieve the third dimension one needs to perform resource intensive joins and form relationships between tables. In order to map programming object classes to the database, incredible programmatic gymnastics are required to achieve the goal.

With MongoDB, there is no rigid database schema you must adhere to. Instead of rows you insert documents. A set of documents is referred to as a collection. Each document can directly model an object class, which in turn greatly facilitates the work of storing and retrieving from the database.

MongoDB has its own rich query language, which can perform tasks similar to what the developer might expect from a legacy RDBMS using SQL. Because MongoDB does not use SQL, it is often referred to as a NoSQL database.

For an excellent introduction to NoSQL, its underlying philosophy and its ramifications, a highly recommended resource can be found in the NoSQL Guide (https://martinfowler.com/nosql.html) on Martin Fowler's website.
 

Installing MongoDB

For the purposes of this book, we focus on the MongoDB Community Edition for the simple reason that it's free of charge. This version is also an excellent way to get your feet wet, so to speak, allowing you to learn about and experiment with MongoDB risk free. Before beginning installation, be sure to check the minimum requirements for your operating system in the MongoDB installation manual: https://docs.mongodb.com/manual/installation/.

On a live server, in a commercial enterprise, it is recommended you use the MongoDB Enterprise Advanced version. You might also consider two cloud-based offerings, MongoDB Atlas or MongoDB Stitch. The former provides a cloud-hosted MongoDB database service. The latter builds upon the former, opening the MongoDB API so that your apps can make calls and receive responses.

Installing MongoDB on Windows

Download and install

To download and install MongoDB on Windows, proceed as follows:
  1. Go to the MongoDB download center at: https://www.mongodb.com/download-center#community.
  2. Select the appropriate operating system where it says Version.
  3. Click on DOWNLOAD (msi):
  1. When prompted, choose Save File.
  2. Click on the saved MSI file to start the installer.
  3. Click OK when the security prompt appears asking to Open Executable File?
  4. Click Run when the security warning appears.
  5. Click Next to start the MongoDB Setup Wizard.
  6. Read the license agreement and click on the checkbox and Next. Note that if you do not accept the license agreement the installation will terminate.
  7. When asked Choose Setup Type, for the purposes of this illustration, select Complete. MongoDB Compass, which is a handy utility which greatly facilitates database management, is automatically installed.
  8. Now that all choices have been made, click on Install and click Yes when the User Account Control security warning pops up.
  1. As of MongoDB v4.0, the installation wizard lets you configure startup options. If you want to have MongoDB start automatically and run in the background, choose Run service as Network Service user. You can also configure the directory where MongoDB stores its data files (Data Directory), and where log files are stored (Log Directory):
  1. Click Next to continue and Finish when the installation completes.

MongoDB Compass

Assuming you elected to install the complete package, MongoDB Compass will auto-launch once the installation completes. You will need to scroll down through its license agreement (separate from the license agreement for MongoDB itself), and click Agree. You can follow and then close the initial help tutorial, and also set Privacy Settings that control whether or not you will be sending crash reports, usage statistics, and requesting automatic updates to/from MongoDB Inc.

This utility is described in more detail in the Chapter 2, Understanding MongoDB Data Structures, We also use this utility to create our first database and collection (see following sections). Here is the Compass screen as seen on Windows:

MongoDB Windows file locations

If using the Windows MSI installer (recommended), the MongoDB program files will be stored here:

C:\Program Files\MongoDB\Server\<version>

You have the option, during the installation process, of specifying the location where the database and log files are stored. Once finished, here is a look at the new directory structure:

If you elected to install MongoDB as a service, it starts automatically, and can be administered just as any Windows service.

The configuration file, which contains the locations of the database and log files, defaults to:

C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg

This file is automatically generated by the installer. By default, here are its contents:

 

Installing MongoDB on Linux

It is important to understand the MongoDB installation process on Linux, even if you are a developer or IT professional and are not using Linux personally, it's extremely likely that the internet-facing server you or your customer use is running Linux. W3Techs (https://w3techs.com/), a company that does web technology surveys, estimates that in 2018, the running on Linux was at 68.1% compared with 32% for Windows.

There are three primary considerations when installing MongoDB on Linux, each of which we will address in turn:

  • Linux based upon Debian and Ubuntu
  • Linux based upon RedHat, Fedora, and CentOS
  • Installing directly from source code
With the bewildering array of Linux distributions currently available, it is difficult to decide which version to feature for the purposes of demonstrating MongoDB on Linux. A significant number of Linux distributions are based on either Debian or Red Hat Linux. Accordingly, this section covers installing MongoDB on both. A website which gives good insight on all reported Linux distributions is DistroWatch (https://distrowatch.com/). Linux Mint (https://linuxmint.com/), although now extremely popular, wasn't included here, as it's Debian-based and not as commercially available as Ubuntu.

Installing on Debian or Ubuntu Linux

Debian Linux (https://www.debian.org/), self-described as the universal operating system, is a free open-source project that uses a fork of the Linux kernel, and draws heavily upon GNU (http://www.gnu.org/software/software.html, for example, GNU Not Unix) software. Ubuntu Linux (https://www.ubuntu.com/) is produced by the Canonical Group Ltd based in South Africa, and is based upon Debian. For the purposes of this book, we will focus on Ubuntu version 18.04, code-named Bionic Beaver, released in April 2018, a designated LTS (Long Term Support) version.

The preferred way to install any given software on Ubuntu is to use a Debian package. Such packages have the extension *.deb and include a script that tells the package management program where to place the pre-compiled binary files as they are extracted. Popular package management programs include synaptic (http://www.nongnu.org/synaptic/, graphical interface, resolves dependencies, and does a lot of "housekeeping"), aptitude (https://help.ubuntu.com/lts/serverguide/aptitude.html, like synaptic but has a textual, command-line menu), and apt-* (that is apt-get (https://linux.die.net/man/8/apt-get), apt-key so on: very fast, command-line only). For the purposes of this section we will use apt-get.

Ubuntu provides its own MongoDB package, which is what gets installed if you simply run sudo apt-get install mongodb. To get the latest "official" version directly from MongoDB, you should follow the procedure outlined as follows. If you already have installed the Ubuntu mongodb package, you will need to first uninstall it before proceeding.

The MongoDB packages available for Ubuntu/Debian include the following:

mongodb-org-server

Primary MongoDB system daemon

mongodb-org-mongos

MongoDB shard routing service

mongodb-org-shell

MongoDB shell

mongodb-org-tools

Provides various mongo* tools for import, export, restore, and so on.

In addition, a composite package, mongodb-org, which contains all four of these packages, is provided.

Package installation

To install MongoDB on an Ubuntu/Debian server, you will need root access. A unique feature of Debian-based Linux distributions is that direct login as root is not allowed for security reasons. Accordingly, you can promote yourself to root using su, or you can precede the various commands with sudo, which instructs the OS to process this command as root.

Please proceed as follows:

  1. Import the public key from the MongoDB key server. This is needed so that the package manager can authenticate the MongoDB package:
  1. Add the MongoDB repository to the Linux server's sources list:
The commands listed should be on one line. We use a backslash (\) to indicate a line of text that is too long to fit the printed page. When typing the command, omit the backslash (\) and do not hit enter until the command has been fully entered.
  1. Refresh the package database from the sources list by running:
      sudo apt-get update
Ubuntu version 18.04 is code-named bionic. You will note this name is used in step #2 here, where the MongoDB repository is added to the sources list. If this source is not found, you will receive an error message:
The repository ... bionic/mongodb-org/4.0 ... does not have a Release file
In this situation, substitute the code name xenial (Ubuntu 16.04) in place of bionic (Ubuntu 18.04).
  1. Install the latest (stable) version of MongoDB. Here, we install only the composite package, which alleviates the need to separately install the four primary packages listed previously:
      sudo apt-get install -y mongodb-org
You will note, at the end of the installation, that the installer creates a user mongodb who belongs to a group mongodb, which is also newly created. This is the system user MongoDB uses when it runs.

Configure and run MongoDB on Ubuntu/Debian

If you followed the procedure outlined in the previous section, a configuration file /etc/mongod.conf will have been auto-generated by the installation script. By default, data files will be placed in /var/lib/mongodb and log files in /var/log/mongodb/mongod.log:

You are now able to perform these operations:

Operation Command
Start | stop | restart the server sudo service mongod start|stop|restart
Get the server status sudo service mongod status
Access MongoDB via the shell (covered later) mongo --host 127.0.0.1:27017
Here you can see the server started, along with its status:
MongoDB installation scripts now automatically bind MongoDB to localhost (IP address 127.0.0.1) for security reasons.

Installing on Red Hat, Fedora, or CentOS Linux

Red Hat, Fedora, and CentOS have a relationship similar to that of Debian and Ubuntu. Red Hat (https://www.redhat.com/en) is the original company behind this distribution, producing its first release in 1995. In addition to making improvements in the graphical interface and overall management of Linux, Red Hat is known for its RPM (Red Hat Package Management) technology. In this corner of the Linux world, packages are bundled into files with the extension *.rpm, and contain installation instructions, which makes the installation, updating, and management of Linux software much easier.

Fedora (https://getfedora.org/) is a free open source version of what is now RHEL (Red Hat Enterprise Linux). Fedora and the Fedora Project are sponsored by Red Hat, and serve as a test bed for innovation, which, when stable, is ported to RHEL. Fedora Linux releases tend to have rapid development cycles and short lifespans. CentOS (https://www.centos.org/) is also affiliated with Red Hat, and is allowed direct use of RHEL source code. The main difference is that CentOS is free, but support is only available via the community (which is to say, you are on your own!). For the purposes of this book we will use CentOS version 7.

Package installation

The MongoDB packages available for RHEL/Fedora/CentOS are exactly the same as those described in preceding sections for Debian/Ubuntu. Also, as described earlier, a composite package called mongodb-org that contains all four packages is available. Because RHEL/Fedora/CentOS packages use RPM for packaging, the tool of choice for installation, updating and management of packages is yum (Yellowdog Updater, Modified).

To install MongoDB on RHEL/Fedora/CentOS Linux distributions, proceed as follows:

  1. Create a repository file for yum in the /etc/yum.repos.d directory. The filename should be like this, mongodb-org-X.Y.repo, where X is the major version number for MongoDB, and Y is the minor release. As an example, for MongoDB version 4.0, the current version as of this writing, the filename would be:
    /etc/yum.repos.d/mongodb-org-4.0.repo:

  1. Install the composite package using: sudo yum install -y mongodb-org:

Configure and run MongoDB on RHEL/Fedora/CentOS

If you followed the procedure outlined previously, a configuration file /etc/mongod.conf will have been auto-generated by the installation script. By default, database files will be placed in /var/lib/mongodb and log files in /var/log/mongodb/mongod.log. Here is an example of the auto-generated file for MongoDB v4.0 on CentOS 7:

You are now able to perform these operations:

Operation Command
Start | stop | restart the server /bin/systemctl start|stop|restart mongod.service
Access MongoDB via the shell (covered later) mongo --host 127.0.0.1:27017

After starting the service, use the command /bin/systemctl status mongod.service to confirm the status of MongoDB:

Installing from source

The beauty of installing MongoDB directly from its source code is that it ensures that you can run MongoDB on any server, and that you have the absolute latest version. Minimum requirements for source installation include:

In addition, there are OS-specific requirements, which are detailed in this table:

Linux Compiler: GCC 4.8.2 or later
Red Hat, and suchlike.

Libraries needed:
glibc-devel
libcurl-devel
openssl-devel
epel-release
python-devel

Ubuntu, and suchlike.

Libraries needed:
build-essential
libffi-dev
libssl-dev
python-dev

macOSX Compiler: Clang 3.4 of XCode 5
Libraries: XCode (especially command line tools)
Windows Compiler: Visual Studio 2013 Update 4 or later
It is highly recommended that you carefully read through the source installation process documentation, which can be found on github.com at this URL: https://github.com/mongodb/mongo/wiki/Build-Mongodb-From-Source.

The source build process does not follow the traditional sequence of configure, make, and make install. Installation is performed using SCons (Software Construction Tool, https://www.scons.org/), which, in turn, uses the programming language Python. Accordingly, after you clone or download the MongoDB source, you will notice many Python scripts and configuration files.

For the purposes of this illustration, we use CentOS 7. To install MongoDB from source, assuming all prerequisites listed previously are met, proceed as follows:

  1. Download the source code from github.com. There are two ways to download the MongoDB source code from github.com:
    1. Download directly from this URL:
      https://github.com/mongodb/mongo/archive/master.zip.
      You would then need to unzip it into a folder such as /home/user/mongo.
    1. If you have installed git, you can clone the repository from a command line terminal as follows:
  1. Change to the newly created (or cloned) mongo directory.
  2. Install pip requirements:
On Windows you would need to run this command:
pip.exe install -r buildscripts\requirements.txt
  1. Build the source code using SCons:

At this point, you can then follow the same steps listed previously to run MongoDB:

  • Create a directory for the database (for example /var/lib/mongo)
  • Create a directory for the log (for example /var/log/mongo)
  • Create a config file, which indicates the locations of the database and log (for example /etc/mongod.conf)
  • Start MongoDB
 

Summary

In this chapter you gained a better understanding of what MongoDB is, why we use it, and what its benefits are. You then learned how to install MongoDB on both Windows and Linux. You learned how to install the pre-compiled binary packages, which use the extension *.deb and are designed for Debian and Ubuntu Linux package manager. In a similar manner, you learned how to install binary packages with the *.rpm extension on Redhat, Fedora, or CentOS Linux distributions. Finally, you learned how to install Linux by directly compiling and installing the source code using SCons technology.

In the next chapter you will learn about MongoDB data structures, data modeling, and how to create a database, collection, and documents.

About the Author
  • Doug Bierer

    Doug Bierer has been hooked on computers since his first program, written on a DEC PDP-8, in 1971. In his wide-ranging career, he has been a professional contract programmer since 1978, having written applications in BASIC, PL/I, assembler, FORTH, C, C++, dBase/FoxBase/Clipper, Pascal, Perl, Java, and PHP. He deployed his first website in 1993 while living in San Francisco. He speaks four languages, has traveled extensively, and now resides in Thailand. He also spent some years doing system administration and TCP/IP networking. Some of his technical works include PHP 7 Programming Cookbook and Learning MongoDB 4.x (Packt), as well as Learning PHP and MySQL, Learning PHP Security and Learning Doctrine (O'Reilly Media).

    Browse publications by this author
Latest Reviews (1 reviews total)
A good book for a beginner.
MongoDB 4 Quick Start Guide
Unlock this book and the full library FREE for 7 days
Start now