Learn MongoDB 4.x
上QQ阅读APP看书,第一时间看更新

Loading the sample data

Although the focus of this chapter is on installation, the remainder of the book teaches you how to implement and manage the major features of MongoDB (for example, replica sets, sharded clusters, and more). In addition, the book focuses on mastering MongoDB queries and developing applications based on Python. Accordingly, we have provided you with a Docker image that includes the following:

  • The latest version of MongoDB preinstalled
  • An Apache web server installation
  • Python 3
  • The PyMongo driver

In this section, we will show you how to restore the source code accompanying this book from a GitHub repository and then bring online the demo environment using Docker and Docker Compose. First, we will walk you through the process of restoring the source code and sample data from the GitHub repository associated with this book.

Installing the source code and sample data using Git

In order to load the sample data and Python code needed to run the examples shown in this book, you will need to either clone the GitHub repository for the book or download and unzip it. The code repository for the book is located here:

https://github.com/PacktPublishing/Learn-MongoDB-4.x

How you can go about restoring the source code and sample data depends on whether or not you have git installed on your computer. Accordingly, we'll look at two techniques, with and without using git.

Here are a series of steps to restore the sample data using git:

  1. From a browser, enter the following URL:
https://github.com/PacktPublishing/Learn-MongoDB-4.x
  1. Locate the green button that says Code.
  2. Click on the down arrow, and select the clipboard icon to copy the repository URL:
  1. Open a Terminal window (or, if using Git for Windows, open the provided Bash shell) and make and/or change to a directory under which you are prepared to clone the sample data repository for this book.
  1. Enter the following command to clone the repository:
git clone https://github.com/PacktPublishing/Learn-MongoDB-4.x.git /path/to/repo
  1. The result should appear as shown in this screenshot. Note that we are using /home/ned/Repos/learn-mongodb in place of /path/to/repo:
  1. The path to this repository will be referred to throughout this book as
    /path/to/repo. Thus, in the example shown in the screenshot, /path/to/repo corresponds to /home/ned/Repos/learn-mongodb.
If you are planning on installing MongoDB and installing the sample data on a Windows computer, it is advised that you install
Git for Windows ( https://gitforwindows.org/). This gives you a Bash shell in which you can enter Linux commands, including git.
Installing source and sample data by downloading and unzipping

If you prefer not to use git, another technique is to simply download the entire repository in the form of a ZIP file. You can then extract its contents wherever is appropriate on your computer. To use this approach, proceed as follows:

  1. From a browser, open this URL:
    https://github.com/PacktPublishing/Learn-MongoDB-4.x.
  2. Locate the green button that says Code.
  3. Click on the down arrow, and select the Download ZIP option.
  4. Use the tools associated with your operating system to open the downloaded ZIP file, and extract it into the subdirectory of your creation.

Now, let's have a look at the directory structure of the sample data.

Understanding the source code directory structure

The source code and sample data for the examples shown in the book are located in these directories under /path/to/repo:

  • chapters: Under this subdirectory, you will find source code for each chapter, by number.
  • www: This directory structure contains source code based upon Django.
  • sample_data: In this directory, you will find JavaScript files that can be executed to populate the database with sample data. There is no need to insert data just yet: as the need arises, you will be instructed in the chapter to insert the sample data appropriate for that section of the book.

Next, we will have a look at creating a demo environment you can use to test the examples shown in the book using Docker and Docker Compose.