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

Doug Bierer

Share book
  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

Doug Bierer

Book details
Book preview
Table of contents
Citations

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

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is MongoDB 4 Quick Start Guide an online PDF/ePUB?
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 Ciencia de la computación & Bases de datos. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789349726

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