Book Image

MongoDB 4 Quick Start Guide

By : Doug Bierer
Book Image

MongoDB 4 Quick Start Guide

By: Doug Bierer

Overview of 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.
Table of Contents (11 chapters)

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: