MongoDB Fundamentals

Book description

Learn how to deploy and monitor databases in the cloud, manipulate documents, visualize data, and build applications running on MongoDB using Node.js

Key Features

  • Learn the fundamentals of NoSQL databases with MongoDB
  • Create, manage, and optimize a MongoDB database in the cloud using Atlas
  • Use a real-world dataset to gain practical experience of handling big data

Book Description

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.

What you will learn

  • Set up and use MongoDB Atlas on the cloud
  • Insert, update, delete, and retrieve data from MongoDB
  • Build aggregation pipelines to perform complex queries
  • Optimize queries using indexes
  • Monitor databases and manage user authorization
  • Improve scalability and performance with sharding clusters
  • Replicate clusters, back up your database, and restore data
  • Create data-driven charts and reports from real-time data

Who this book is for

This book is designed for people who are new to MongoDB. It is suitable for developers, database administrators, system administrators, and cloud architects who are looking to use MongoDB for smooth data processing in the cloud. Although not necessary, basic knowledge of a general programming language and experience with other databases will help you grasp the topics covered more easily.

Table of contents

  1. MongoDB Fundamentals
  2. Preface
    1. About the Book
      1. About the Authors
      2. Who This Book Is For
      3. About the Chapters
      4. Conventions
      5. Before You Begin
      6. Installing MongoDB
      7. Editors and IDEs
      8. Downloading and Installing Visual Studio Code
      9. Downloading Node.js
        1. Windows
        2. Mac
        3. Linux
      10. Verifying the Installation
      11. Installing the Code Bundle
      12. Get in Touch
      13. Please Leave a Review
  3. 1. Introduction to MongoDB
    1. Introduction
    2. Database Management Systems
      1. Relational Database Management Systems
      2. NoSQL Database Management Systems
      3. Comparison
    3. Introduction to MongoDB
    4. MongoDB Editions
      1. Migrating Community Edition to Enterprise Edition
    5. The MongoDB Deployment Model
    6. Managing MongoDB
      1. Self-Managed
      2. Managed Service: Database as a Service
    7. MongoDB Atlas
      1. MongoDB Atlas Benefits
      2. Cloud Providers
      3. Availability Zones
      4. Regions
      5. MongoDB Supported Regions and Availability Zones
      6. Atlas Tiers
        1. MongoDB Atlas Pricing
        2. Cluster Cost Estimation
      7. Exercise 1.01: Setting Up a MongoDB Atlas Account
    8. MongoDB Atlas Organizations, Projects, Users, and Clusters
      1. Organizations
      2. Exercise 1.02: Setting Up a MongoDB Atlas Organization
      3. Projects
      4. Exercise 1.03: Creating a MongoDB Atlas Project
      5. MongoDB Clusters
      6. Exercise 1.04: Setting Up Your First Free MongoDB Cluster on Atlas
      7. Connecting to Your MongoDB Atlas Cluster
      8. MongoDB Elements
      9. Documents
      10. Document Structures
      11. Collections
      12. Understanding MongoDB Databases
      13. Creating a Database
      14. Creating a Collection
      15. Creating a Collection Using Document Insertion
      16. Creating Documents
        1. Inserting a Single Document
        2. Inserting Multiple Documents
        3. Fetching Documents from MongoDB
        4. Formatting the find Output Using the pretty() Method
      17. Activity 1.01: Setting Up a Movies Database
    9. Summary
  4. 2. Documents and Data Types
    1. Introduction
    2. Introduction to JSON
      1. JSON Syntax
      2. JSON Data Types
      3. JSON and Numbers
      4. JSON and Dates
      5. Exercise 2.01: Creating Your Own JSON Document
    3. BSON
    4. MongoDB Documents
      1. Documents and Flexibility
    5. MongoDB Data Types
      1. Strings
      2. Numbers
      3. Booleans
      4. Objects
      5. Exercise 2.02: Creating Nested Objects
      6. Arrays
      7. Exercise 2.03: Using Array Fields
      8. Null
      9. ObjectId
      10. Dates
      11. Timestamps
      12. Binary Data
    6. Limits and Restrictions on Documents
      1. Document Size Limit
      2. Nesting Depth Limit
    7. Field Name Rules
      1. Exercise 2.04: Loading Data into an Atlas Cluster
      2. Activity 2.01: Modeling a Tweet into a JSON Document
    8. Summary
  5. 3. Servers and Clients
    1. Introduction
    2. Network Access
      1. Network Protocols
        1. Public versus Private IP Addresses
      2. Domain Name Server
      3. Transmission Control Protocol
      4. The Wire Protocol
      5. Network Access Configuration
      6. The IP Access List
      7. Temporary Access
      8. Network Peering
      9. Exercise 3.01: Enabling Network Access
    3. Database Access
      1. User Authentication
        1. Username Storage
        2. Username Authentication
        3. Configuring Authentication in Atlas
        4. Temporary Users
      2. Database Privileges and Roles
        1. Predefined Roles
        2. Configuring Built-In Roles in Atlas
        3. Advanced Privileges
      3. Exercise 3.02: Configuring Database Access
        1. Configuring Custom Roles
    4. The Database Client
      1. Connection Strings
        1. The Mongo Shell
      2. Exercise 3.03: Connecting to the Cloud Database Using the Mongo Shell
      3. MongoDB Compass
      4. MongoDB Drivers
      5. Exercise 3.04: Connecting to a MongoDB Cloud Database Using the Python Driver
    5. Server Commands
      1. Physical Structure
        1. Database Files
        2. Database Metrics
      2. Logical Structure
      3. Server Commands
      4. Exercise 3.05: Creating a Database View Object
      5. Activity 3.01: Managing Your Database Users
    6. Summary
  6. 4. Querying Documents
    1. Introduction
    2. MongoDB Query Structure
    3. Basic MongoDB Queries
      1. Finding Documents
      2. Using findOne()
      3. Exercise 4.01: Using find() and findOne() Without a Condition
      4. Choosing the Fields for the Output
      5. Finding the Distinct Fields
      6. Counting the Documents
        1. count()
        2. countDocuments()
        3. estimatedDocumentCount()
    4. Conditional Operators
      1. Equals ($eq)
      2. Not Equal To ($ne)
      3. Greater Than ($gt) and Greater Than or Equal To ($gte)
      4. Less Than ($lt) and Less Than or Equal To ($lte)
      5. In ($in) and Not In ($nin)
      6. Exercise 4.02: Querying for Movies of an Actor
    5. Logical Operators
      1. $and operator
      2. $or Operator
      3. $nor Operator
      4. $not Operator
      5. Exercise 4.03: Combining Multiple Queries
    6. Regular Expressions
      1. Using the caret (^) operator
        1. Using the dollar ($) operator
        2. Case-Insensitive Search
    7. Query Arrays and Nested Documents
      1. Finding an Array by an Element
      2. Finding an Array by an Array
        1. Searching an Array with the $all Operator
      3. Projecting Array Elements
        1. Projecting Matching Elements Using ($)
        2. Projecting Matching Elements by their Index Position ($slice)
      4. Querying Nested Objects
      5. Querying Nested Object Fields
      6. Exercise 4.04: Projecting Nested Object Fields
    8. Limiting, Skipping, and Sorting Documents
      1. Limiting the Result
      2. Limit and Batch Size
        1. Positive Limit with Batch Size
        2. Negative Limits and Batch Size
      3. Skipping Documents
      4. Sorting Documents
      5. Activity 4.01: Finding Movies by Genre and Paginating Results
    9. Summary
  7. 5. Inserting, Updating, and Deleting Documents
    1. Introduction
    2. Inserting Documents
      1. Inserting Multiple Documents
      2. Inserting Duplicate Keys
      3. Inserting without _id
    3. Deleting Documents
      1. Deleting Using deleteOne()
      2. Exercise 5.01: Deleting One of Many Matched Documents
      3. Deleting Multiple Documents Using deleteMany()
      4. Deleting Using findOneAndDelete()
      5. Exercise 5.02: Deleting a Low-Rated Movie
    4. Replacing Documents
      1. _id Fields Are Immutable
      2. Upsert Using Replace
        1. Why Use Upsert?
      3. Replacing Using findOneAndReplace()
      4. Replace versus Delete and Re-Insert
    5. Modify Fields
      1. Updating a Document with updateOne()
        1. Modifying More Than One Field
        2. Multiple Documents Matching a Condition
        3. Upsert with updateOne()
      2. Updating a Document with findOneAndUpdate()
        1. Returning a New Document in Response
        2. Sorting to Find a Document
      3. Exercise 5.03: Updating the IMDb and Tomatometer Rating
      4. Updating Multiple Documents with updateMany()
    6. Update Operators
      1. Set ($set)
      2. Increment ($inc)
      3. Multiply ($mul)
      4. Rename ($rename)
      5. Current Date ($currentDate)
      6. Removing Fields ($unset)
      7. Setting When Inserted ($setOnInsert)
      8. Activity 5.01: Updating Comments for Movies
    7. Summary
  8. 6. Updating with Aggregation Pipelines and Arrays
    1. Introduction
      1. Updating with an Aggregation Pipeline (MongoDB 4.2)
        1. Stage 1 ($set)
        2. Stage 2 ($set)
        3. Stage 3 ($project)
    2. Updating Array Fields
      1. Exercise 6.01: Adding Elements to Arrays
      2. Adding Multiple Elements
      3. Sort Array
      4. An Array as a Set
      5. Exercise 6.02: New Category of Classic Movies
      6. Removing Array Elements
        1. Removing the First or Last Element ($pop)
        2. Removing All Elements
        3. Removing Matched Elements
      7. Updating Array Elements
      8. Exercise 6.03: Updating the Director's Name
      9. Activity 6.01: Adding an Actor's Name to the Cast
    3. Summary
  9. 7. Data Aggregation
    1. Introduction
    2. aggregate Is the New find
      1. Aggregate Syntax
      2. The Aggregation Pipeline
      3. Pipeline Syntax
      4. Creating Aggregations
      5. Exercise 7.01: Performing Simple Aggregations
      6. Exercise 7.02: Aggregation Structure
    3. Manipulating Data
      1. The Group Stage
      2. Accumulator Expressions
      3. Exercise 7.03: Manipulating Data
      4. Exercise 7.04: Selecting the Title from Each Movie Category
    4. Working with Large Datasets
      1. Sampling with $sample
      2. Joining Collections with $lookup
      3. Outputting Your Results with $out and $merge
      4. Exercise 7.05: Listing the Most User-Commented Movies
    5. Getting the Most from Your Aggregations
      1. Tuning Your Pipelines
      2. Filter Early and Filter Often
      3. Use Your Indexes
      4. Think about the Desired Output
      5. Aggregation Options
      6. Exercise 7.06: Finding Award-Winning Documentary Movies
      7. Activity 7.01: Putting Aggregations into Practice
    6. Summary
  10. 8. Coding JavaScript in MongoDB
    1. Introduction
    2. Connecting to the Driver
      1. Introduction to Node.js
      2. Getting the MongoDB Driver for Node.js
      3. The Database and Collection Objects
      4. Connection Parameters
      5. Exercise 8.01: Creating a Connection with the Node.js Driver
    3. Executing Simple Queries
      1. Creating and Executing find Queries
      2. Using Cursors and Query Results
      3. Exercise 8.02: Building a Node.js Driver Query
    4. Callbacks and Error Handling in Node.js
    5. Callbacks in Node.js
      1. Basic Error Handling in Node.js
      2. Exercise 8.03: Error Handling and Callbacks with the Node.js Driver
    6. Advanced Queries
      1. Inserting Data with the Node.js Driver
      2. Updating and Deleting Data with the Node.js Driver
      3. Writing Reusable Functions
      4. Exercise 8.04: Updating Data with the Node.js Driver
      5. Reading Input from the Command Line
      6. Creating an Interactive Loop
      7. Exercise 8.05: Handling Inputs in Node.js
      8. Activity 8.01: Creating a Simple Node.js Application
    7. Summary
  11. 9. Performance
    1. Introduction
    2. Query Analysis
      1. Explaining the Query
      2. Viewing Execution Stats
      3. Identifying Problems
      4. Linear Search
    3. Introduction to Indexes
    4. Creating and Listing Indexes
      1. Listing Indexes on a Collection
      2. Index Names
      3. Exercise 9.01: Creating an Index Using MongoDB Atlas
    5. Query Analysis after Indexes
    6. Hiding and Dropping Indexes
      1. Dropping Multiple Indexes
      2. Hiding an Index
      3. Exercise 9.02: Dropping an Index Using Mongo Atlas
    7. Type of Indexes
      1. Default Indexes
      2. Single-Key Indexes
      3. Compound Indexes
      4. Multikey Indexes
      5. Text Indexes
      6. Indexes on Nested Documents
      7. Wildcard Indexes
    8. Properties of Indexes
      1. Unique Indexes
      2. Exercise 9.03: Creating a Unique Index
      3. TTL Indexes
      4. Exercise 9.04: Creating a TTL index using Mongo Shell
      5. Sparse Indexes
      6. Exercise 9.05: Creating a Sparse Index Using Mongo Shell
      7. Partial Indexes
      8. Exercise 9.06: Creating a Partial Index Using the Mongo Shell
      9. Case-Insensitive Indexes
      10. Exercise 9.07: Creating a Case-Insensitive Index Using the Mongo Shell
    9. Other Query Optimization Techniques
      1. Fetch Only What You Need
      2. Sorting Using Indexes
      3. Fitting Indexes in the RAM
      4. Index Selectivity
      5. Providing Hints
      6. Optimal Indexes
      7. Activity 9.01: Optimizing a Query
    10. Summary
  12. 10. Replication
    1. Introduction
      1. High-Availability Clusters
        1. Cluster Nodes
        2. Share-Nothing
        3. Cluster Names
        4. Replica Sets
        5. Primary-Secondary
      2. The Oplog
      3. Replication Architecture
      4. Cluster Members
      5. The Election Process
      6. Exercise 10.01: Checking Atlas Cluster Members
    2. Client Connections
      1. Connecting to a Replica Set
      2. Single-Server Connections
      3. Exercise 10.02: Checking the Cluster Replication
      4. Read Preference
      5. Write Concern
    3. Deploying Clusters
      1. Atlas Deployment
      2. Manual Deployment
      3. Exercise 10.03: Building Your Own MongoDB Cluster
      4. Enterprise Deployment
    4. Cluster Operations
      1. Adding and Removing Members
        1. Adding a Member
        2. Removing a Member
        3. Reconfiguring a Cluster
      2. Failover
        1. Failover (Outage)
        2. Rollback
        3. Switchover (Stepdown)
      3. Exercise 10.04: Performing Database Maintenance
      4. Activity 10.01: Testing a Disaster Recovery Procedure for a MongoDB Database
    5. Summary
  13. 11. Backup and Restore in MongoDB
    1. Introduction
    2. The MongoDB Utilities
    3. Exporting MongoDB Data
      1. Using mongoexport
      2. mongoexport Options
      3. Exercise 11.01: Exporting MongoDB Data
    4. Importing Data into MongoDB
      1. Using mongoimport
      2. mongoimport Options
      3. Exercise 11.02: Loading Data into MongoDB
    5. Backing up an Entire Database
      1. Using mongodump
      2. mongodump Options
      3. Exercise 11.03: Backing up MongoDB
    6. Restoring a MongoDB Database
      1. Using mongorestore
      2. The mongorestore Options
      3. Exercise 11.04: Restoring MongoDB Data
      4. Activity 11.01: Backup and Restore in MongoDB
    7. Summary
  14. 12. Data Visualization
    1. Introduction
      1. Exploring Menus and Tabs
      2. Dashboards
      3. Data Sources
      4. Exercise 12.01: Working with Data Sources
      5. Data Source Permissions
      6. Building Charts
        1. Fields
      7. Types of Charts
      8. Bar and Column Charts
      9. Exercise 12.02: Creating a Bar Chart to Display Movies
      10. Circular Charts
      11. Exercise 12.03: Creating a Pie Chart Graph from the Movies Collection
      12. Geospatial Charts
      13. Exercise 12.04: Creating a Geospatial Chart
    2. Complex Charts
      1. Preprocessing and Filtering Data
        1. Filtering Data
        2. Adding Custom Fields
        3. Changing Fields
    3. Channels
      1. Aggregation and Binning
      2. Exercise 12.05: Binning Values for a Bar Graph
    4. Integration
      1. Embedded Charts
      2. Exercise 12.06: Adding Charts to HTML pages
      3. Activity 12.01: Creating a Sales Presentation Dashboard
    5. Summary
  15. 13. MongoDB Case Study
    1. Introduction
    2. Fair Bay City Council
    3. Fair Bay City Bikes
      1. Proposal Highlights
        1. Dockless Bikes
        2. Ease of Use
        3. Real-Time Tracking
        4. Maintenance and Care
    4. Technical Discussions and Decisions
      1. Quick Rollout
        1. Cost Effective
        2. Flexible
    5. Database Design
      1. Users
      2. Vehicles
      3. Rides
      4. Ride Logs
    6. Use Cases
      1. User Finds Available Bikes
        1. User Unlocks a Bike
        2. User Locks the Bike
        3. System Logs the Geographical Coordinates of Rides
        4. System Sends Bikes for Maintenance
        5. Technician Performs Fortnightly Maintenance
        6. Generating Stats
    7. Summary
  16. Appendix
    1. 1. Introduction to MongoDB
      1. Activity 1.01: Setting Up a Movies Database
    2. 2. Documents and Data Types
      1. Activity 2.01: Modeling a Tweet into a JSON Document
    3. 3. Servers and Clients
      1. Activity 3.01: Managing Your Database Users
    4. 4. Querying Documents
      1. Activity 4.01: Finding Movies by Genre and Paginating Results
    5. 5. Inserting, Updating, and Deleting Documents
      1. Activity 5.01: Updating Comments for Movies
    6. 6. Updating with Aggregation Pipelines and Arrays
      1. Activity 6.01: Adding an Actor's Name to the Cast
    7. 7. Data Aggregation
      1. Activity 7.01: Putting Aggregations into Practice
    8. 8. Coding JavaScript in MongoDB
      1. Activity 8.01: Creating a Simple Node.js Application
    9. 9. Performance
      1. Activity 9.01: Optimizing a Query
    10. 10. Replication
      1. Activity 10.01: Testing a Disaster Recovery Procedure for a MongoDB Database
    11. 11. Backup and Restore in MongoDB
      1. Activity 11.01: Backup and Restore in MongoDB
    12. 12. Data Visualization
      1. Activity 12.01: Creating a Sales Presentation Dashboard

Product information

  • Title: MongoDB Fundamentals
  • Author(s): Amit Phaltankar, Juned Ahsan, Michael Harrison, Liviu Nedov
  • Release date: December 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781839210648