Mastering MongoDB 3.x An expert's guide to building fault-tolerant MongoDB applications Alex Giamas BIRMINGHAM - MUMBAI Mastering MongoDB 3.x Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: November 2017 Production reference: 1151117 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-260-8 www.packtpub.com Credits Author Copy Editors Alex Giamas Safis Editing Reviewers Project Coordinator Juan Tomás Oliva Ramos Nidhi Joshi Nilap Shah Commissioning Editor Proofreader Amey Varangaonkar Safis Editing Acquisition Editor Indexer Vinay Argekar Aishwarya Gangawane Content Development Editor Graphics Mayur Pawanikar Tania Dutta Technical Editor Production Coordinator Prasad Ramesh Shantanu Zagade About the Author Alex Giamas is a Senior Software Engineer at the Department for International Trade, UK Government. He has also worked as a consultant for various startups. He is an experienced professional in systems engineering, NoSQL and big data technologies, with experience spanning from co-founding a digital health startup to Fortune 15 companies. He has been developing using MongoDB since 2009 and early 1.x versions, using it for several projects around data storage and analytical processing. He has been developing in Apache Hadoop since 2007 while working on its incubation. He has worked with a wide array of NoSQL and big data technologies, building scalable and highly available distributed software systems in C++, Java, Ruby and Python. Alex holds an MSc from Carnegie Mellon University in Information Networking and has attended professional courses in Stanford University. He is a graduate from National Technical University of Athens, Greece in Electrical and Computer Engineering. He is a MongoDB Certified developer, a Cloudera Certified Developer for Apache Hadoop and Data Science essentials. He publishes regularly for the past 4 years at InfoQ in NoSQL, big data and data science topics. I would like to thank my parents for their support and advice all these years. I would like to thank my fiancé Mary for her patience and support throughout the time, days and nights, weekdays and weekends I spent writing this book. About the Reviewers Juan Tomás Oliva Ramos is an environmental engineer from the University of Guanajuato, Mexico, with a master's degree in administrative engineering and quality. He has more than 5 years of experience in the management and development of patents, technological innovation projects, and the development of technological solutions through the statistical control of processes. He has been a teacher of statistics, entrepreneurship, and the technological development of projects since 2011. He became an entrepreneur mentor and started a new department of technology management and entrepreneurship at Instituto Tecnológico Superior de Purisima del Rincon Guanajuato, Mexico. Juan is an Alfaomega reviewer and has worked on the book Wearable Designs for Smart Watches, Smart TVs and Android Mobile Devices. Juan has also developed prototypes through programming and automation technologies for the improvement of operations, which have been registered for patents. I want to thank God for giving me wisdom and humility to review this book. I thank Packt for giving me the opportunity to review this amazing book and to collaborate with a group of committed people I want to thank my beautiful wife, Brenda, our two magic princesses (Maria Regina and Maria Renata) and our next member (Angel Tadeo), all of you, give me the strength, happiness, and joy to start a new day. Thanks for being my family. Nilap Shah is a lead software consultant with experience across various fields and technologies. He is an expert in .NET, Uipath (robotics), and MongoDB. He is a certified MongoDB developer and DBA. He is a technical writer as well as a technical speaker. He also provides MongoDB corporate training. Currently, Nilap is working as a lead MongoDB consultant and provides solutions with MongoDB (DBA and developer projects). His LinkedIn profile can be found at https:/ /www.linkedin.com/in/nilap- shah-8b6780a/ and you can reach him on WhatsApp at +91-9537047334. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1783982608. If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Preface 1 Chapter 1: MongoDB – A Database for the Modern Web 6 Web history 7 Web 1.0 7 Web 2.0 9 Web 3.0 11 SQL and NoSQL evolution 11 MongoDB evolution 13 Major feature set for versions 1.0 and 1.2 13 Version 2 13 Version 3 14 Version 3+ 14 MongoDB for SQL developers 16 MongoDB for NoSQL developers 17 MongoDB key characteristics and use cases 18 Key characteristics 18 What is the use case for MongoDB? 19 MongoDB criticism 21 MongoDB configuration and best practices 22 Operational best practices 23 Schema design best practices 25 Best practices for write durability 26 Best practices for replication 27 Best practices for sharding 27 Best practices for security 28 Best practices for AWS 28 Reference documentation 29 MongoDB documentation 29 Packt references 29 Further reading 30 Summary 30 Chapter 2: Schema Design and Data Modeling 31 Relational schema design 32 MongoDB schema design 32