You are previewing MongoDB Cookbook - Second Edition.
O'Reilly logo
MongoDB Cookbook - Second Edition

Book Description

Harness the latest features of MongoDB 3 with this collection of 80 recipes – from managing cloud platforms to app development, this book is a vital resource

About This Book

  • Get to grips with the latest features of MongoDB 3

  • Interact with the MongoDB server and perform a wide range of query operations from the shell

  • From administration to automation, this cookbook keeps you up to date with the world’s leading NoSQL database

  • Who This Book Is For

    This book is engineered for anyone who is interested in managing data in an easy and efficient way using MongoDB. You do not need any prior knowledge of MongoDB, but it would be helpful if you have some programming experience in either Java or Python.

    What You Will Learn

  • Install, configure, and administer MongoDB sharded clusters and replica sets

  • Begin writing applications using MongoDB in Java and Python languages

  • Initialize the server in three different modes with various configurations

  • Perform cloud deployment and introduce PaaS for Mongo

  • Discover frameworks and products built to improve developer productivity using Mongo

  • Take an in-depth look at the Mongo programming driver APIs in Java and Python

  • Set up enterprise class monitoring and backups of MongoDB

  • In Detail

    MongoDB is a high-performance and feature-rich NoSQL database that forms the backbone of the systems that power many different organizations – it’s easy to see why it’s the most popular NoSQL database on the market. Packed with many features that have become essential for many different types of software professionals and incredibly easy to use, this cookbook contains many solutions to the everyday challenges of MongoDB, as well as guidance on effective techniques to extend your skills and capabilities.

    This book starts with how to initialize the server in three different modes with various configurations. You will then be introduced to programming language drivers in both Java and Python. A new feature in MongoDB 3 is that you can connect to a single node using Python, set to make MongoDB even more popular with anyone working with Python. You will then learn a range of further topics including advanced query operations, monitoring and backup using MMS, as well as some very useful administration recipes including SCRAM-SHA-1 Authentication. Beyond that, you will also find recipes on cloud deployment, including guidance on how to work with Docker containers alongside MongoDB, integrating the database with Hadoop, and tips for improving developer productivity.

    Created as both an accessible tutorial and an easy to use resource, on hand whenever you need to solve a problem, MongoDB Cookbook will help you handle everything from administration to automation with MongoDB more effectively than ever before.

    Style and approach

    Every recipe is explained in a very simple set-by-step manner yet is extremely comprehensive.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. MongoDB Cookbook Second Edition
      1. Table of Contents
      2. MongoDB Cookbook Second Edition
      3. Credits
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Installing and Starting the Server
        1. Introduction
        2. Installing single node MongoDB
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Starting a single node instance using command-line options
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Single node installation of MongoDB with options from the config file
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Connecting to a single node in the Mongo shell with JavaScript
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Connecting to a single node using a Java client
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Connecting to a single node using a Python client
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Starting multiple instances as part of a replica set
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        9. Connecting to the replica set in the shell to query and insert data
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        10. Connecting to the replica set to query and insert data from a Java client
          1. Getting ready
          2. How to do it…
          3. How it works…
        11. Connecting to the replica set to query and insert data using a Python client
          1. Getting ready
          2. How to do it…
          3. How it works…
        12. Starting a simple sharded environment of two shards
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        13. Connecting to a shard in the shell and performing operations
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      9. 2. Command-line Operations and Indexes
        1. Introduction
        2. Creating test data
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Performing simple querying, projections, and pagination from Mongo shell
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Updating and deleting data from the shell
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Creating index and viewing plans of queries
          1. Getting ready
          2. How to do it…
          3. How it works…
            1. Analyzing the plan
            2. Improving the query execution time
            3. Improvement using indexes
            4. Improvement using covered indexes
            5. Some caveats of index creations
        6. Creating a background and foreground index in the shell
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Creating and understanding sparse indexes
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Expiring documents after a fixed interval using the TTL index
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        9. Expiring documents at a given time using the TTL index
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      10. 3. Programming Language Drivers
        1. Introduction
        2. Executing query and insert operations with PyMongo
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Executing update and delete operations using PyMongo
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Implementing aggregation in Mongo using PyMongo
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Executing MapReduce in Mongo using PyMongo
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Executing query and insert operations using a Java client
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also…
        7. Executing update and delete operations using a Java client
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Implementing aggregation in Mongo using a Java client
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Executing MapReduce in Mongo using a Java client
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      11. 4. Administration
        1. Introduction
        2. Renaming a collection
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Viewing collection stats
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Viewing database stats
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. How it works…
        5. Manually padding a document
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. The mongostat and mongotop utilities
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Getting current executing operations and killing them
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Using profiler to profile operations
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Setting up users in Mongo
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        10. Interprocess security in Mongo
          1. Getting ready
          2. How to do it…
          3. There's more…
        11. Modifying collection behavior using the collMod command
          1. Getting ready
          2. How it works…
          3. How it works…
        12. Setting up MongoDB as a windows service
          1. Getting ready
          2. How to do it…
        13. Replica set configurations
          1. Getting ready
            1. Elections in a replica set
            2. Basic configuration for a replica set
          2. How to do it…
          3. How it works…
            1. Replica set member as an arbiter
            2. Priority of replica set members
            3. Hidden, slave delayed, and build index configuration
          4. There's more…
        14. Stepping down as primary from the replica set
          1. Getting ready
          2. How to do it…
          3. How it works…
        15. Exploring the local database of a replica set
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        16. Understanding and analyzing oplogs
          1. Getting ready
          2. How to do it…
          3. How it works…
        17. Building tagged replica sets
          1. Getting ready
          2. How to do it…
          3. How it works…
            1. WriteConcern in tagged replica sets
            2. ReadPreference in tagged replica sets
        18. Configuring the default shard for non-sharded collections
          1. Getting ready
          2. How to do it…
          3. How it works…
        19. Manual split and migration of chunks
          1. Getting ready
          2. How to do it…
          3. How it works…
        20. Domain-driven sharding using tags
          1. Getting ready
          2. How to do it…
          3. How it works…
        21. Exploring the config database in a sharded setup
          1. Getting ready
          2. How to do it…
          3. How it works…
      12. 5. Advanced Operations
        1. Introduction
        2. Atomic find and modify operations
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Implementing atomic counters in Mongo
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Implementing server-side scripts
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Creating and tailing a capped collection cursors in MongoDB
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Converting a normal collection to a capped collection
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Storing binary data in Mongo
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Storing large data in Mongo using GridFS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        9. Storing data to GridFS from Java client
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        10. Storing data to GridFS from Python client
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        11. Implementing triggers in Mongo using oplog
          1. Getting ready
          2. How to do it…
          3. How it works…
        12. Flat plane 2D geospatial queries in Mongo using geospatial indexes
          1. Getting ready
          2. How to do it…
          3. How it works…
        13. Spherical indexes and GeoJSON compliant data in Mongo
          1. Getting ready
          2. How to do it…
          3. How it works…
        14. Implementing full text search in Mongo
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        15. Integrating MongoDB for full text search with Elasticsearch
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
      13. 6. Monitoring and Backups
        1. Introduction
        2. Signing up for MMS and setting up an MMS monitoring agent
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Managing users and groups in MMS console
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Monitoring instances and setting up alerts on MMS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Setting up monitoring alerts in MMS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Back up and restore data in Mongo using out-of-the-box tools
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Configuring MMS Backup service
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Managing backups in MMS Backup service
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      14. 7. Deploying MongoDB on the Cloud
        1. Introduction
        2. Setting up and managing the MongoLab account
          1. How to do it…
          2. How it works…
        3. Setting up a sandbox MongoDB instance on MongoLab
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Performing operations on MongoDB from MongoLab GUI
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Setting up MongoDB on Amazon EC2 manually
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Setting up MongoDB using the Docker containers
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      15. 8. Integration with Hadoop
        1. Introduction
        2. Executing our first sample MapReduce job using the mongo-hadoop connector
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also…
        3. Writing our first Hadoop MapReduce job
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Running MapReduce jobs on Hadoop using streaming
          1. Getting ready…
          2. How it works…
          3. How to do it…
        5. Running a MapReduce job on Amazon EMR
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      16. 9. Open Source and Proprietary Tools
        1. Introduction
        2. Developing using spring-data-mongodb
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Accessing MongoDB using JPA
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Accessing MongoDB over REST
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Installing a GUI-based client, MongoVUE, for MongoDB
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
      17. A. Concepts for Reference
        1. Write concern and its significance
          1. Setting up a replica set
        2. Read preference for querying
          1. Knowing the internals
      18. Index