Preface
Aging legacy database technologies used in current relational database management systems (RDBMS) are no longer able to handle the needs of modern web applications. MongoDB, built upon a NoSQL foundation, has taken the world by storm and is rapidly gaining market share over inadequate RDBMS-based websites. It is critical for DevOps, technical managers, and those whose careers are in transition to gain knowledge on how to build dynamic database-driven web applications based upon MongoDB. This book takes a deep dive into management and the application development of a web application that uses MongoDB 4.x as its database.
The book takes a hands-on approach, building a set of useful Python classes, starting with simple operations, and adding more complexity as the book progresses. When you have finished going through the book, you'll have mastered not only basic day-to-day MongoDB database operations, but will have gained a firm understanding of development using complex MongoDB features such as replication, sharding, and the aggregation framework. Complex queries are presented in both JavaScript form as well as the equivalent in Python. The JavaScript queries presented will allow DBAs to manage and manipulate data directly, as well as being able to generate ad hoc reports for management. Throughout the book, practical examples of each concept are covered in detail, along with potential errors and gotchas.
All examples in the book are based upon realistic real-world scenarios ranging from a small company that sells sweets online, all the way to a start-up bank that offers micro-financing to customers worldwide. With each scenario, you'll learn how to develop a usable MongoDB database structure based on realistic customer requirements. Each major part of the book, based upon a different scenario, guides you through progressively more complex tasks, ranging from generating simple reports, to directing queries through a global network of sharded MongoDB database clusters.
Who this book is for
This book is designed for anyone who roughly fits any of these descriptions:
- DevOps: Information technology (IT) staff involved with development and/or operations pertaining to a MongoDB 4.x database.
- Database administrators (DBAs): Database administrators who need to know how to install, configure, and manage a MongoDB 4.x database.
- Systems administrators: Technical staff in charge of one or more servers who might be called upon to perform DBA activities and generate ad hoc reports for management.
- Technical managers: Managers who need to keep up with and understand the terminology and tasks of the DevOps they manage.
- Students: Anybody attending an institution of higher education, or anybody whose career is in transition, looking for a market niche from which they can carve out a decent career with a solid future.
- IT professionals: Any professional associated with the IT industry looking to transition from legacy RDBMS-based applications to modern NoSQL-based apps.
What this book covers
Chapter 1, Introducing MongoDB 4.x, provides a general introduction to MongoDB 4.x with a focus on new features and a brief high-level overview of the technology and the differences between MongoDB 4.x and MongoDB 3.
Chapter 2, Setting Up MongoDB 4.x, covers MongoDB 4.x and Python programming language drivers. In addition, this chapter shows you how to set up a demo test environment based upon Docker, in which you can practice working with MongoDB.
Chapter 3, Essential MongoDB Administration Techniques, shows how to perform administration critical to a properly functioning database using the mongo shell.
Chapter 4, Fundamentals of Database Design, describes moving from a set of customer requirements to a working MongoDB database design.
Chapter 5, Mission-Critical MongoDB Database Tasks, presents a series of common tasks and then shows you how to define domain classes that perform critical database access services including performing queries, adding, editing, and deleting documents.
Chapter 6, Using AJAX and REST to Build a Database-Driven Website, moves on to what is needed to have the application respond to AJAX queries and REST requests.
Chapter 7, Advanced MongoDB Database Design, covers how to define a dataset with complex requirements. You are also shown how MongoDB can be integrated with the popular Django web framework.
Chapter 8, Using Documents with Embedded Lists and Objects, covers working with the documents designed in the previous chapter, learning how to handle create, read, update and delete operations that involve embedded lists (arrays) and objects.
Chapter 9, Handling Complex Queries in MongoDB, introduces you to the aggregation framework, a feature unique to MongoDB. In this chapter, you also learn about map-reduce, geospatial queries, generating financial reports, and performing risk analysis.
Chapter 10, Working with Complex Documents across Collections, starts with a brief discussion on how to handle monetary data, after which the focus shifts to working with complex documents across multiple collections. Lastly, GridFS technology is introduced as a way of handling large files and storing documents directly in the database.
Chapter 11, Administering MongoDB Security, focuses on the administration needed to secure the database by creating database users, implement role-based access control and implement transport layer (SSL/TLS) security based upon x.509 certificates.
Chapter 12, Developing in a Secured Environment, looks into how to write applications that access a secured database using role-based access control and TLS security using x.509 certificates.
Chapter 13, Deploying a Replica Set, starts with an overview of MongoDB replication, after which you'll learn how to configure and deploy a replica set.
Chapter 14, Replica Set Runtime Management and Development, focuses on replica set management, monitoring, backup, and restore. In addition, it addresses how your application program code might change when accessing a replica set.
Chapter 15, Deploying a Sharded Cluster, starts with an overview of sharding, after which you'll learn how to configure and deploy a sharded cluster.
Chapter 16, Sharded Cluster Management and Development, covers how to manage a sharded cluster as well as learning about the impact of sharding on application program code.
To get the most out of this book
In order to test the sample queries and coding examples presented in this book, you need the following:
Minimum recommended hardware:
- Desktop PC or laptop
- 2 GB free disk space
- 4 GB of RAM
- 500 Kbps or faster internet connection
Software requirements:
- OS (Linux or Mac): Docker, Docker Compose, Git (optional)
- OS (Windows): Docker for Windows and Git for Windows
In order to successfully understand and work through the examples presented in this book, the following background knowledge will be helpful:
- Basic knowledge of Python and/or Javascript
- Basic knowledge of what is a database and what a database is used for
- Working knowledge of HTML and web applications in general
- Knowledge of Docker and Linux is useful but not mandatory as all necessary commands are shown
Python 3.x, the PyMongo driver, an Apache web server, and various Python libraries are already installed in the Docker container used for the book. Chapter 2, Setting Up MongoDB 4.x, gives you instructions on how to install the demo environment used in the book.
If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
Download the example code files
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
- Log in or register at www.packt.com.
- Select the Support tab.
- Click on Code Downloads.
- Enter the name of the book in the Search box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR/7-Zip for Windows
- Zipeg/iZip/UnRarX for Mac
- 7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Learn-MongoDB-4.x. In case there's an update to the code, it will be updated on the existing GitHub repository. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/9781789619386_ColorImages.pdf.
Conventions used
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database collection and field names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Finally, the $sort stage reorders the final results by the match field _id (that is, the country code)."
A block of code is set as follows:
db.bookings.aggregate([
{ $match : { "bookingInfo.paymentStatus" : "confirmed" } },
{ $group: { "_id" : "$customer.customerAddr.country",
"total" : { $sum : "$totalPrice" } } },
{ $sort : { "_id" : 1 } }
]);
If a line of code or a command needs to be all on a single line, but the book's page width prevents this, the line will be broken up into two lines. A backslash (\) is placed at the break point. The remainder of the line appears indented on the next line as follows:
this.command('has', 'many', 'arguments', 'and', \
'would', 'occupy', 'a', 'single', 'line')
Any command-line input or output is written as follows:
cd /path/to/repo
docker-compose build
docker-compose up -d
docker exec -it learn-mongo-server-1 /bin/bash
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "You can also select Fill in connection fields individually, in which case these are the two tab screens you can use."
Get in touch
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.