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

Condividi libro
  1. 192 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e 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

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
MongoDB 4 Quick Start Guide è disponibile online in formato PDF/ePub?
Sì, puoi accedere a MongoDB 4 Quick Start Guide di Doug Bierer in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Ciencia de la computación e Bases de datos. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
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...

Indice dei contenuti