MongoDB Cookbook - Second Edition
By Dasadia Cyrus and Nayak Amol
()
About this ebook
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.
Related to MongoDB Cookbook - Second Edition
Related ebooks
Apache Maven Cookbook Rating: 0 out of 5 stars0 ratingsNode Cookbook: Second Edition Rating: 3 out of 5 stars3/5Flask Framework Cookbook Rating: 5 out of 5 stars5/5Apache Spark for Data Science Cookbook Rating: 0 out of 5 stars0 ratingsPostgreSQL High Performance Cookbook Rating: 0 out of 5 stars0 ratingsGit Version Control Cookbook Rating: 4 out of 5 stars4/5Apache Hive Cookbook Rating: 0 out of 5 stars0 ratingsElixir Cookbook Rating: 0 out of 5 stars0 ratingsLearning Flask Framework Rating: 4 out of 5 stars4/5Instant MongoDB Rating: 0 out of 5 stars0 ratingsGetting MEAN with Mongo, Express, Angular, and Node Rating: 5 out of 5 stars5/5Full Stack Python Security: Cryptography, TLS, and attack resistance Rating: 0 out of 5 stars0 ratingsNode.js Design Patterns Rating: 4 out of 5 stars4/5Parallel Programming with Python Rating: 0 out of 5 stars0 ratingsScala Functional Programming Patterns Rating: 0 out of 5 stars0 ratingsFlask By Example Rating: 0 out of 5 stars0 ratingsNode.js in Practice Rating: 0 out of 5 stars0 ratingsMastering Apache Camel Rating: 0 out of 5 stars0 ratingsMariaDB High Performance Rating: 0 out of 5 stars0 ratingsDeploying Node.js Rating: 5 out of 5 stars5/5TypeScript: Modern JavaScript Development Rating: 0 out of 5 stars0 ratingsKafka in Action Rating: 0 out of 5 stars0 ratingsBuilding Web Applications with Python and Neo4j Rating: 0 out of 5 stars0 ratingsPostgreSQL 9.0 High Performance Rating: 4 out of 5 stars4/5Mastering Node.js Rating: 1 out of 5 stars1/5Get Programming with Scala Rating: 0 out of 5 stars0 ratingsDistributed Computing in Java 9 Rating: 0 out of 5 stars0 ratingsBuilding RESTful Python Web Services Rating: 5 out of 5 stars5/5Git Best Practices Guide Rating: 0 out of 5 stars0 ratings
Enterprise Applications For You
Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Bitcoin For Dummies Rating: 4 out of 5 stars4/5Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Scrivener For Dummies Rating: 4 out of 5 stars4/5QuickBooks 2023 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsEnterprise AI For Dummies Rating: 3 out of 5 stars3/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/5QuickBooks 2024 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsThe Basics of Hacking and Penetration Testing: Ethical Hacking and Penetration Testing Made Easy Rating: 4 out of 5 stars4/5Excel 2016 For Dummies Rating: 4 out of 5 stars4/5SharePoint 2016 For Dummies Rating: 5 out of 5 stars5/5Excel Formulas and Functions 2020: Excel Academy, #1 Rating: 4 out of 5 stars4/5ChatGPT Ultimate User Guide - How to Make Money Online Faster and More Precise Using AI Technology Rating: 0 out of 5 stars0 ratingsSystems Thinking: Managing Chaos and Complexity: A Platform for Designing Business Architecture Rating: 4 out of 5 stars4/5Learning Python Rating: 5 out of 5 stars5/5Learn Windows PowerShell in a Month of Lunches Rating: 0 out of 5 stars0 ratingsPowerpoint 2013 Tips & Tricks Rating: 0 out of 5 stars0 ratings101 Ready-to-Use Excel Formulas Rating: 4 out of 5 stars4/5QuickBooks Online For Dummies Rating: 0 out of 5 stars0 ratingsQuickBooks Online For Dummies Rating: 0 out of 5 stars0 ratingsMrExcel XL: The 40 Greatest Excel Tips of All Time Rating: 4 out of 5 stars4/5Excel Workbook For Dummies Rating: 4 out of 5 stars4/5Managing Humans: Biting and Humorous Tales of a Software Engineering Manager Rating: 4 out of 5 stars4/5
Reviews for MongoDB Cookbook - Second Edition
0 ratings0 reviews
Book preview
MongoDB Cookbook - Second Edition - Dasadia Cyrus
www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
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 files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <questions@packtpub.com> if you are having a problem with any aspect of the book, and we will do our best to address it.
Chapter 1. Installing and Starting the Server
In this chapter, we will cover the following recipes:
Installing single node MongoDB
Starting a single node instance using the command-line options
Installing single node MongoDB with options from the config file
Connecting to a single node in the Mongo shell with JavaScript
Connecting to a single node from a Java client
Connecting to a single node from a Python client
Starting multiple instances as part of a replica set
Connecting to the replica set in the shell to query and insert data
Connecting to the replica set to query and insert data from a Java client
Connecting to the replica set to query and insert data using a Python client
Starting a simple sharded environment of two shards
Connecting to a shard in the shell and performing operations
Introduction
In this chapter, we will look at starting up the MongoDB server. Though it is a cakewalk to start the server with default settings for development purposes, there are numerous options available to fine-tune the start up behavior. We will start the server as a single node and then introduce various configuration options. We will conclude this chapter by setting up a simple replica set and running a sharded cluster. So, let's get started with installing and setting up the MongoDB server in the easiest way possible for simple development purposes.
Installing single node MongoDB
In this recipe, we will look at installing MongoDB in the standalone mode. This is the simplest and quickest way to start a MongoDB server, but it is seldom used for production use cases. However, this is the most common way to start the server for development purposes. In this recipe, we will start the server without looking at a lot of other startup options.
Getting ready
Well, assuming that we have downloaded the MongoDB binaries from the download site, extracted it, and have the resulting bin directory in the operating system's path variable. (This is not mandatory, but it really becomes convenient after doing so.) The binaries can be downloaded from http://www.mongodb.org/downloads after selecting your host operating system.
How to do it…
Create the directory, /data/mongo/db (or any of your choice). This will be our database directory, and it needs to have permission to write to it by the mongod (the mongo server process) process.
We will start the server from the console with the data directory, /data/mongo/db, as follows:
> mongod --dbpath /data/mongo/db
How it works…
If you see the following line on the console, you have successfully started the server:
[initandlisten] waiting for connections on port 27017
Starting a server can't get easier than this. Despite the simplicity in starting the server, there are a lot of configuration options that can be used to tune the behavior of the server on startup. Most of the default options are sensible and need not be changed. With the default values, the server should be listening to port 27017 for new connections, and the logs will be printed out to the standard output.
See also
There are times where we would like to configure some options on server startup. In the Installing single node MongoDB recipe, we will use some more start up options.
Starting a single node instance using command-line options
In this recipe, we will see how to start a standalone single node server with some command-line options. We will see an example where we want to do the following:
Start the server listening to port 27000
Logs should be written to /logs/mongo.log
The database directory is /data/mongo/db
As the server has been started for development purposes, we don't want to preallocate full-size database files. (We will soon see what this means.)
Getting ready
If you have already seen and executed the Installing single node MongoDB recipe, you need not do anything different. If all these prerequisites are met, we are good for this recipe.
How to do it…
The /data/mongo/db directory for the database and /logs/ for the logs should be created and present on your filesystem with appropriate permissions to write to it.
Execute the following command:
> mongod --port 27000 --dbpath /data/mongo/db –logpath /logs/mongo.log --smallfiles
How it works…
Ok, this wasn't too difficult and is similar to the previous recipe, but we have some additional command-line options this time around. MongoDB actually supports quite a few options at startup, and we will see a list of the most common and important ones in my opinion: