MongoDB 4 Quick Start Guide
eBook - ePub

MongoDB 4 Quick Start Guide

Learn the skills you need to work with the world's most popular NoSQL database

  1. 192 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

MongoDB 4 Quick Start Guide

Learn the skills you need to work with the world's most popular NoSQL database

About this book

A fast paced guide that will help you to create, read, update and delete data using MongoDB

Key Features

  • Create secure databases with MongoDB
  • Manipulate and maintain your database
  • Model and use data in a No SQL environment with MongoDB

Book Description

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.

What you will learn

  • Get a standard MongoDB database up and running quickly
  • Perform simple CRUD operations on the database using the MongoDB command shell
  • Set up a simple aggregation pipeline to return subsets of data grouped, sorted, and filtered
  • Safeguard your data via replication and handle massive amounts of data via sharding
  • Publish data from a web form to the database using a program language driver
  • Explore the basic CRUD operations performed using the PHP MongoDB driver

Who this book is for

Web developers, IT professionals and Database Administrators (DBAs) who want to learn how to create and manage MongoDB databases.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access MongoDB 4 Quick Start Guide by Doug Bierer in PDF and/or ePUB format, as well as other popular books in Computer Science & Data Mining. We have over one million books available in our catalogue for you to explore.

Information

Maintaining MongoDB Performance

The features covered in this chapter will show you how to improve performance by creating and using indexes, and how to safeguard data using replication and backups. In addition, you will learn how to handle massive amounts of data with sharding.
The topics that are going to be covered in this chapter are as follows:
  • Indexes
  • Simple backup and restore
  • Replication
  • Sharding

Indexes

Command summary:
db.collection.createIndex( { <fieldname> : ( 1 | -1 ) } );
Creating indexes is an easy way to improve MongoDB performance at the collection level. Indexes can be created on a single field, multiple fields, or embedded fields within arrays or objects. When you issue a query which involves the indexed field, MongoDB is able to use information stored in the index rather than having to do a full scan of all database documents. In this sense, you can think of the index as a shortcut which saves time when producing query results.
There are three index types supported by MongoDB: single field (https://docs.mongodb.com/manual/core/index-single/#single-field-indexes), compound (https://docs.mongodb.com/manual/core/index-compound/#compound-indexes), and multi-key (https://docs.mongodb.com/manual/core/index-multikey/#multikey-indexes).
Each of these can be defined as ascending or descending. In addition, there is an auto-generated default index on the _id field.

Single field indexes

For this illustration, we will use the sweetscomplete.purchases collection, which we described in Chapter 5, Building Complex Queries Using Aggregation. To avoid flipping pages, here is the first document in this collection to remind you of its structure:
Consider the following query:
db.purchases.find( {},
{_id:0, date:1, "customer.name":1, "customer.country":1})
.sort( {"customer.country":1, date:-1} );
If you frequently run queries which involve the date field, then it would improve the performance of find() and sort() to index this field. To create the index, you can use the collection method createIndex(), specifying the field on which to create the index as an argument. You would then use 1 for ascending and -1 for descending. The following example creates a descending index on the date field:
You can also create single field indexes on embedded documents. In the following example, we are creating an ascending index on customer.country:
You can add the explain("executionStats") method to the cursor (for example, db.collection.find().explain()) to reveal performance statistics for your query before and after the index has been created.

Compound indexes

Compound indexes are useful when you wish to create an index on more than one field. To illustrate the use of this type of index, we will turn our attention again to the sweetscomplete.purchases collection. In this example, we issue a query which gives us a list of customers outside of the United States:
db.purchases.find(
{"customer.country": {$not:/US/}},
{_id:0,"customer.name":1,"customer.state_province":1,"customer.country":1}
).sort(
{"customer.country":1,"customer.state_province":1,"customer.name":1}
);
When we tack the explain("executionStats") method to the query, we learn that this query takes 18 milliseconds to complete (only a fragment of the entire dump is shown):
We can now create an index on the fields that are included in the sort:
You will note that the execution time, after re-running the query, now takes 12 milliseconds (as shown in this fragment). It is worth mentioning that this is actually quite a significant speed improvement, given the small size of the collection. Imagine how much performance increase you would see on a large dataset!

Multi-key indexes

Multi-key indexes (https://docs.mongodb.com/manual/core/index-multikey/#multikey-indexes) are needed if you are dealing with documents with array fields. As an example, let's assume that sweetscomplete.customers now contains a new field called purch_history, with a list of purchase dates. We wish to generate a count of customers who have purchased items in June 2018. Granted we could generate the same information by scanning the purchases collection, but to illustrate the need for a multi-key index, let's examine this sample query:
db.customers.find( {
purch_history: {
$elemMatch: { $regex: /^2018-06/ }
}
} ).count();
As you can see, from the query, the purch_history array is scanned using the $elemMatch array operator. To create the index, simply specify the field, in this case purch_history:

Simple backup and restore

Command summary (from the command line):
  • mongodump
  • mongorestore
Hopefully there is no need to stress how important it is to maintain a regular backup schedule. That being said, it is worth mentioning that doing a simple restore can actually cause problems in a properly constructed system of replicas or shards (covered later on in this chapter). In this section, we will address a simple backup...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Introducing MongoDB
  8. Understanding MongoDB Data Structures
  9. Using the MongoDB Shell
  10. Developing with Program Language Drivers
  11. Building Complex Queries Using Aggregation
  12. Maintaining MongoDB Performance
  13. Securing MongoDB
  14. Getting from a Web Form to MongoDB
  15. Using Docker
  16. Other Books You May Enjoy