0%

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