Reader small image

You're reading from  MongoDB Fundamentals

Product typeBook
Published inDec 2020
PublisherPackt
ISBN-139781839210648
Edition1st Edition
Tools
Concepts
Right arrow
Authors (4):
Amit Phaltankar
Amit Phaltankar
author image
Amit Phaltankar

Amit Phaltankar is a software developer and a blogger experienced in building lightweight and efficient software components. He specializes in wiring web-based applications and handling large-scale data sets using traditional SQL, NoSQL, and big data technologies. He is experienced in many technology stacks and loves learning and adapting to newer technology trends. Amit is passionate about improving his skill set and loves guiding and grooming his peers and contributing to blogs. He is also an author of MongoDB Fundamentals.
Read more about Amit Phaltankar

Juned Ahsan
Juned Ahsan
author image
Juned Ahsan

Juned Ahsan is a software professional with more than 14 years of experience. He has built software products and services for companies and clients such as Cisco, Nuamedia, IBM, Nokia, Telstra, Optus, Pizzahut, AT&T, Hughes, Altran, and others. Juned has a vast experience in building software products and architecting platforms of different sizes from scratch. He loves to help and mentor others and is a top 1% contributor on StackOverflow. He is passionate about cognitive CX, cloud computing, artificial intelligence, and NoSQL databases.
Read more about Juned Ahsan

Michael Harrison
Michael Harrison
author image
Michael Harrison

Michael Harrison started his career at the Australian telecommunications leader Telstra. He worked across their networks, big data, and automation teams. He is now a lead software developer and the founding member of Southbank Software, a Melbourne based startup that builds tools for the next generation of database technologies.
Read more about Michael Harrison

Liviu Nedov
Liviu Nedov
author image
Liviu Nedov

Liviu Nedov is a senior consultant with more than 20 years of experience in database technologies. He has provided professional and consulting services to customers in Australia and Europe. Throughout his career, he has designed and implemented large enterprise projects for customers like Wotif Group, Xstrata Copper/Glencore, and the University of Newcastle and Energy, Queensland. He is currently working at Data Intensity, which is the largest multi-cloud service provider for applications, databases, and business intelligence. In recent years, he is actively involved in MongoDB NoSQL database projects, database migrations, and cloud DBaaS (Database as a Service) projects.
Read more about Liviu Nedov

View More author details
Right arrow

About the Book

MongoDB is one of the most popular database technologies for handling large collections of data. This book will help MongoDB beginners develop the knowledge and skills to create databases and process data efficiently.

Unlike other MongoDB books, MongoDB Fundamentals dives into cloud computing from the very start – showing you how to get started with Atlas in the first chapter. You will discover how to modify existing data, add new data into a database, and handle complex queries by creating aggregation pipelines. As you progress, you'll learn about the MongoDB replication architecture and configure a simple cluster. You will also get to grips with user authentication, as well as techniques for backing up and restoring data. Finally, you'll perform data visualization using MongoDB Charts.

You will work on realistic projects that are presented as bitesize exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way. Many of these mini-projects are based around a movie database case study, while the last chapter acts as a final project where you will use MongoDB to solve a real-world problem based on a bike-sharing app.

By the end of this book, you'll have the skills and confidence to process large volumes of data and tackle your own projects using MongoDB.

About the Authors

Amit Phaltankar is a software developer and a blogger with more than 13 years of experience in building lightweight and efficient software components. He specializes in wiring web-based applications as well as handling large-scale data sets using traditional SQL, NoSQL, and big data technologies. He has work experience in a wide range of technology stack and loves learning and adapting to newer technology trends. Amit has a huge passion for improving his skill set and also loves guiding and grooming his peers and contributing to blogs. During the last 6 years, he has effectively used MongoDB in various ways to build faster systems.

Juned Ahsan is a software professional with more than 14 years of experience. He has built software products and services for companies and clients such as Cisco, Nuamedia, IBM, Nokia, Telstra, Optus, Pizza Hut, AT&T, Hughes, Altran, and others. Juned has vast experience in building software products and architecting platforms of different sizes from scratch. He loves to help and mentor others and is a top 1% contributor on Stack Overflow. Juned is passionate about cognitive CX, cloud computing, artificial intelligence, and NoSQL databases.

Michael Harrison started his career at the Australian telecommunications leader Telstra. He worked across their networks, big data, and automation teams. He is now a lead software developer and the founding member of Southbank Software, a Melbourne based startup that builds tools for the next generation of database technologies. As a full-stack engineer, Michael led the development of an open-sourced, platform-agnostic IDE for MongoDB (dbKoda ), as well as a Blockchain-enabled database built on top of MongoDB, called ProvenDB. Both these products were exhibited at the MongoDB World conference in New York. Given that Michael owns a pair of MongoDB socks, it's safe to say he's an enthusiast.

Liviu Nedov is a senior consultant with more than 20 years of experience in database technologies. He has provided professional and consulting services to customers in Australia and Europe. Throughout his career, he has designed and implemented large enterprise projects for customers like Wotif Group, Xstrata Copper/Glencore, and the University of Newcastle and Energy, Queensland. He is currently working at Data Intensity, which is the largest multi-cloud service provider for applications, databases, and business intelligence. In recent years, he is actively involved in MongoDB, NoSQL database projects, database migrations, and cloud DBaaS (Database as a Service) projects.

Who This Book Is For

MongoDB Fundamentals is targeted at readers with a basic technical background who are approaching MongoDB for the first time. Any database, JavaScript, or JSON experience will be useful, but not required. MongoDB Fundamentals may briefly dip into these technologies as well as more advanced topics, but no background knowledge is needed to gain value from this book.

About the Chapters

Chapter 1, Introduction to MongoDB, contains the history and context of MongoDB, essential concepts, and a guide to setting up your first MongoDB instance.

Chapter 2, Documents and Data Types, will teach you about the critical components in MongoDB data and commands.

Chapter 3, Servers and Clients, provides you with the information needed to manage MongoDB access and connections, including the creation of databases and collections.

Chapter 4, Querying Documents, is where we get to the core of MongoDB: querying the database. This chapter provides hands-on exercises to get you working with the query syntax, operators, and modifiers.

Chapter 5, Inserting, Updating, and Deleting Documents, expands on querying, allowing you to change a query into an update, modifying existing data.

Chapter 6, Updating with Aggregation Pipelines and Arrays, covers more complex update operations, using pipelines and bulk updates.

Chapter 7, Data Aggregation, demonstrates one of MongoDB's most powerful advanced features, allowing you to create reusable, complex query pipelines that can't be solved with more straightforward queries.

Chapter 8, Coding JavaScript in MongoDB, takes you from direct database interactions to a method more commonly found in the real world: queries from an application. In this chapter, you will create a simple Node.js application that can programmatically interact with MongoDB.

Chapter 9, Performance, provides you with the information and tools to ensure your queries are running effectively, primarily by using indexes and execution plans.

Chapter 10, Replication, takes a closer look at the standard MongoDB configurations you may encounter in production environments, namely clusters and replica sets.

Chapter 11, Backup and Restore, covers the information needed as part of managing database redundancy and migration. This is integral for database administration but is also useful for loading sample data and development life cycles.

Chapter 12, Data Visualization, explains how you can turn raw data into meaningful visualizations that aid in discovering and communicating insights within the data.

Chapter 13, MongoDB Case Study, is an end-of-course case study that will tie together all the skills covered in the previous chapters in a real-world example.

Conventions

Code words in text form, database and collection names, file and folder names, shell commands, and user input use the following formatting: "The db.myCollection.findOne() command will return the first document from myCollection."

Smaller blocks of sample code and their output will be formatted in blocks like this:

use sample_mflix
var pipeline = []
var options  = {}
var cursor   = db.movies.aggregate(pipeline, options);

In most cases, where the output is a separate block, it will be formatted as a figure like this:

Figure 0.1: Output as a figure

Figure 0.1: Output as a figure

Often, at the beginning of chapters, key new terms will be introduced. In these cases, the following formatting will be used: "The aggregate command operates on a collection like the other Create, Read, Update, Delete (CRUD) commands like so."

Before You Begin

As mentioned earlier, MongoDB is more than just a database. It's a vast and sprawling set of tools and libraries. So, before we dive headfirst into MongoDB, we'd better make sure we're fully equipped for the adventure.

Installing MongoDB

  1. Download the MongoDB Community tarball (tgz) from https://www.mongodb.com/try/download/community. In the Available Downloads section, select the current (4.4.1) version, your platform, and click download.
  2. Place the downloaded tgz file in any folder of your choice and extract it. On a Linux-based operating system, including macOS, the tgz file can be extracted to a folder using Command Prompt. Open the terminal, navigate to the directory where you copied the tgz file, and issue the following command:
         tar -zxvf mongodb-macos-x86_64-4.4.1.tgz

    Note that the name of the tgz can vary based on your operating system and the version you have downloaded. If you peep into the extracted folder, you will find all the MongoDB binaries, including mongod and mongo, are placed in the bin directory.

  3. The executables, such as mongod and mongo, are the launchers for the MongoDB database and Mongo Shell respectively. To be able to launch them from anywhere, you will need to add these commands to the PATH variable or copy the binaries into the /usr/local/bin directory. Alternatively, you can keep the binaries where they are and create symbolic links of these binaries into the /usr/local/bin directory. To create symbolic links, you need to open Terminal, navigate into the MongoDB installation directory, and execute this command:
         sudo ln -s /full_path/bin/* /usr/local/bin/
  4. To run MongoDB locally, you must create a data directory. Execute the next command and create the data directory in any folder you want:
         mkdir -p ~/mytools/mongodb
  5. To verify whether the installation was successful, run MongoDB locally. For that, you need to use the mongo command and provide the path of the data directory:
         mongod --dbpath ~/mytools/mongodb

    Once you execute this command, MongoDB starts on the default of port of 27017, and you should see MongoDB boot logs; the last line contains msg":"Waiting for connections, which indicates that the database is up and waiting for clients, such as a Mongo shell, to make connections.

  6. Finally, you need to verify the Mongo shell by connecting it to the database. The next command is used to start the Mongo shell with default configurations:
    mongo

    Executing this command, you should see the shell prompt is started. By default, the shell connects to the database running on the localhost 27017 port. In the coming chapters, you will learn how to connect the shell to a MongoDB Atlas cluster.

  7. Detailed instructions on installing MongoDB on Windows or any specific operating system can be found in MongoDB's official installation manual, located at https://docs.mongodb.com/manual/installation/.

Editors and IDEs

The MongoDB shell allows you to directly interact with databases by merely typing commands into the console. However, this method will only get you so far and will end up being more of a burden as you perform more advanced operations. For this reason, we recommend having a text editor ready to write your commands down, and these can then be copied into the shell. Although any text editor will work, if you don't already have a preference, we recommend Visual Studio Code as it has some helpful plugins for MongoDB. That being said, whatever tools you are comfortable with will be more than enough for this book.

There is also a wide array of tools for MongoDB that will help you along your way. We don't prescribe a particular tool as the best way to learn, but we recommend doing some searches online to find tools and plugins that provide you with extra value during the learning process.

Downloading and Installing Visual Studio Code

Let's go ahead and get set up with a proper JavaScript IDE. You can choose whichever one you like, of course, but we are going to stick with Visual Studio Code for the initial chapters. It's an approachable editor dedicated to web technologies and is available on every major operating system:

  1. First, you need to acquire the installation package. This can be done in different ways depending upon your operating system, but the most direct way is to visit the Visual Studio Code website using https://code.visualstudio.com/.
  2. The website should detect your operating system and present you with a button that allows the direct download of a stable build. Of course, you can choose a different version by clicking the down arrow for additional options:
    Figure 0.2: Download prompt for Visual Studio Code

    Figure 0.2: Download prompt for Visual Studio Code

  3. Once downloaded, the installation will depend upon your operating system. Again, depending on your chosen operating system, the installation will differ slightly:

    macOS: The downloaded file is a .ZIP archive. You will need to unzip the package to expose the .APP application file.

    Windows: An executable .EXE file is downloaded to your local machine.

    Linux: Depending upon your download choice, you will have either a .DEB or .RPM package downloaded to your local environment.

  4. With the installer package downloaded, you now have to run an installation routine dependent upon our chosen operating system:

    macOS: Drag the Visual Studio Code .APP to the Applications folder. This will make it available through macOS interface utilities such as Spotlight Search.

    Windows: Simply run the executable installer and follow the instructions to set everything up.

    Linux: There are many possibilities here; refer to your operating system instructions for the proper installation of the .DEB or .RPM package.

  5. With Visual Studio Code installed, you now only need to pin it to the Taskbar, the Dock, or any other operating system mechanism that allows quick and easy access to the program.

That's it. Visual Studio Code is now available to use.

So far, we have had a look at a variety of integrated development environments available for use today when working with JavaScript. We also downloaded and installed Visual Studio Code, a modern JavaScript IDE from Microsoft. We'll now see why it is important to use proper filesystem preparation when beginning a new JavaScript project.

Downloading Node.js

Node.js is open source and you can download it from its official website for all platforms. It supports all three major platforms: Windows, Linux, and macOS.

Windows

Visit their official website and download the latest stable .msi installer. The process is very simple. Just execute the .msi file and follow the instructions to install it on the system. There will be some prompts about accepting license agreements. You have to accept those and then click on Finish. That's it.

Mac

The installation processes for Windows and Mac are very similar. You have to download the .pkg file from the official website and execute it. Then, follow the instructions. You may have to accept the license agreement. After that, follow the prompts to finish the installation process.

Linux

To install Node.js on Linux, execute the following commands in the same order they are mentioned:

  • $ cd /tmp
  • $ wget http://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.gz
  • $ tar xvfz node-v8.11.2-linux-x64.tar.gz
  • $ sudo mkdir -p /usr/local/nodejs
  • $ sudo mv node-v8.11.2-linux-x64/* /usr/local/nodejs

Note that you will need to use sudo in the last two commands only if you are not logged in as the admin. Here, you first change the current active directory to the temporary directory (tmp) of the system. Second, you download the tar package of node from their official distribution directory. Third, you extract the tar package to the tmp directory. This directory contains all the compiled and executable files. Fourth, you create a directory in the system for Node.js. In the last command, you are moving all the complied and executable files of the package to that directory.

Verifying the Installation

After the installation process, you can verify whether it is installed properly on the system by executing the following command:

$ node -v && npm -v

It will output the currently installed version of Node.js and npm:

Figure 0.3: Installed versions of Node.js and npm

Figure 0.3: Installed versions of Node.js and npm

Here, it shows that the 8.11.2 version of Node.js is installed on the system, as is the 5.6.0 version of npm.

Installing the Code Bundle

Download the code files from GitHub at https://github.com/PacktPublishing/MongoDB-Fundamentals. The files here contain the exercises, activities, and some intermediate code for each chapter. This can be a useful reference when you become stuck.

You can use the Download ZIP option to download the complete code as a ZIP file. Alternatively, you can use the git command to check out the repository, as shown in the next snippet:

git clone https://github.com/PacktPublishing/MongoDB-Fundamentals.git

Get in Touch

Feedback from our readers is always welcome.

General feedback: If you have any questions about this book, please mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you could report this to us. Please visit www.packtpub.com/support/errata and complete the form.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you could provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Please Leave a Review

Let us know what you think by leaving a detailed, impartial review on Amazon. We appreciate all feedback – it helps us continue to make great products and help aspiring developers build their skills. Please spare a few minutes to give your thoughts – it makes a big difference to us.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MongoDB Fundamentals
Published in: Dec 2020Publisher: PacktISBN-13: 9781839210648
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (4)

author image
Amit Phaltankar

Amit Phaltankar is a software developer and a blogger experienced in building lightweight and efficient software components. He specializes in wiring web-based applications and handling large-scale data sets using traditional SQL, NoSQL, and big data technologies. He is experienced in many technology stacks and loves learning and adapting to newer technology trends. Amit is passionate about improving his skill set and loves guiding and grooming his peers and contributing to blogs. He is also an author of MongoDB Fundamentals.
Read more about Amit Phaltankar

author image
Juned Ahsan

Juned Ahsan is a software professional with more than 14 years of experience. He has built software products and services for companies and clients such as Cisco, Nuamedia, IBM, Nokia, Telstra, Optus, Pizzahut, AT&T, Hughes, Altran, and others. Juned has a vast experience in building software products and architecting platforms of different sizes from scratch. He loves to help and mentor others and is a top 1% contributor on StackOverflow. He is passionate about cognitive CX, cloud computing, artificial intelligence, and NoSQL databases.
Read more about Juned Ahsan

author image
Michael Harrison

Michael Harrison started his career at the Australian telecommunications leader Telstra. He worked across their networks, big data, and automation teams. He is now a lead software developer and the founding member of Southbank Software, a Melbourne based startup that builds tools for the next generation of database technologies.
Read more about Michael Harrison

author image
Liviu Nedov

Liviu Nedov is a senior consultant with more than 20 years of experience in database technologies. He has provided professional and consulting services to customers in Australia and Europe. Throughout his career, he has designed and implemented large enterprise projects for customers like Wotif Group, Xstrata Copper/Glencore, and the University of Newcastle and Energy, Queensland. He is currently working at Data Intensity, which is the largest multi-cloud service provider for applications, databases, and business intelligence. In recent years, he is actively involved in MongoDB NoSQL database projects, database migrations, and cloud DBaaS (Database as a Service) projects.
Read more about Liviu Nedov