MongoDB-manual.pdf - MongoDB Documentation Release 3.0.2 MongoDB Documentation Project 2 Contents 1 Introduction to MongoDB 1.1 What is MongoDB 2

MongoDB-manual.pdf - MongoDB Documentation Release 3.0.2...

This preview shows page 1 out of 920 pages.

You've reached the end of your free preview.

Want to read all 920 pages?

Unformatted text preview: MongoDB Documentation Release 3.0.2 MongoDB Documentation Project April 16, 2015 2 Contents 1 Introduction to MongoDB 1.1 What is MongoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Install MongoDB 2.1 Recommended Operating Systems for Production Deployments 2.2 Other Supported Operating Systems . . . . . . . . . . . . . . . 2.3 Installation Guides . . . . . . . . . . . . . . . . . . . . . . . . 2.4 First Steps with MongoDB . . . . . . . . . . . . . . . . . . . . 2.5 Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . MongoDB CRUD Operations 3.1 MongoDB CRUD Introduction 3.2 MongoDB CRUD Concepts . . 3.3 MongoDB CRUD Tutorials . . 3.4 MongoDB CRUD Reference . . 3 4 5 6 7 3 3 5 5 5 5 48 54 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 . 55 . 58 . 91 . 127 Data Models 4.1 Data Modeling Introduction . . . . 4.2 Data Modeling Concepts . . . . . . 4.3 Data Model Examples and Patterns 4.4 Data Model Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 141 143 149 166 Administration 5.1 Administration Concepts . 5.2 Administration Tutorials . 5.3 Administration Reference 5.4 Production Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 181 218 281 307 Security 6.1 Security Introduction 6.2 Security Concepts . 6.3 Security Tutorials . . 6.4 Security Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 313 315 329 397 Aggregation 7.1 Aggregation Introduction 7.2 Aggregation Concepts . . 7.3 Aggregation Examples . . 7.4 Aggregation Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 425 429 442 459 . . . . . . . . . . . . i 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 471 476 506 541 Replication 9.1 Replication Introduction 9.2 Replication Concepts . . 9.3 Replica Set Tutorials . . 9.4 Replication Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 547 551 587 637 10 Sharding 10.1 Sharding Introduction . . 10.2 Sharding Concepts . . . . 10.3 Sharded Cluster Tutorials 10.4 Sharding Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 647 653 675 721 11 Frequently Asked Questions 11.1 FAQ: MongoDB Fundamentals . . . . . . . 11.2 FAQ: MongoDB for Application Developers 11.3 FAQ: The mongo Shell . . . . . . . . . . . 11.4 FAQ: Concurrency . . . . . . . . . . . . . . 11.5 FAQ: Sharding with MongoDB . . . . . . . 11.6 FAQ: Replication and Replica Sets . . . . . 11.7 FAQ: MongoDB Storage . . . . . . . . . . . 11.8 FAQ: Indexes . . . . . . . . . . . . . . . . . 11.9 FAQ: MongoDB Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729 729 732 742 744 748 753 757 762 764 12 Release Notes 12.1 Current Stable Release . . . . . 12.2 Previous Stable Releases . . . . 12.3 Other MongoDB Release Notes 12.4 MongoDB Version Numbers . . 9 Indexes 8.1 Index Introduction 8.2 Index Concepts . . 8.3 Indexing Tutorials 8.4 Indexing Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 769 804 898 899 13 About MongoDB Documentation 13.1 License . . . . . . . . . . . . . . . . . . . 13.2 Editions . . . . . . . . . . . . . . . . . . . 13.3 Version and Revisions . . . . . . . . . . . 13.4 Report an Issue or Make a Change Request 13.5 Contribute to the Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901 901 901 902 902 902 ii . . . . . . . . . . . . . . . . . . . . MongoDB Documentation, Release 3.0.2 See About MongoDB Documentation (page 901) for more information about the MongoDB Documentation project, this Manual and additional editions of this text. Note: This version of the PDF does not include the reference section, see MongoDB Reference Manual1 for a PDF edition of all MongoDB Reference Material. 1 Contents 1 MongoDB Documentation, Release 3.0.2 2 Contents CHAPTER 1 Introduction to MongoDB Welcome to MongoDB. This document provides a brief introduction to MongoDB and some key concepts. See the installation guides (page 5) for information on downloading and installing MongoDB. 1.1 What is MongoDB MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling. 1.1.1 Document Database A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents. The advantages of using documents are: • Documents (i.e. objects) correspond to native data types in many programming languages. • Embedded documents and arrays reduce need for expensive joins. • Dynamic schema supports fluent polymorphism. 3 MongoDB Documentation, Release 3.0.2 1.1.2 Key Features High Performance MongoDB provides high performance data persistence. In particular, • Support for embedded data models reduces I/O activity on database system. • Indexes support faster queries and can include keys from embedded documents and arrays. High Availability To provide high availability, MongoDB’s replication facility, called replica sets, provide: • automatic failover. • data redundancy. A replica set (page 547) is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability. Automatic Scaling MongoDB provides horizontal scalability as part of its core functionality. • Automatic sharding (page 647) distributes data across a cluster of machines. • Replica sets can provide eventually-consistent reads for low-latency high throughput deployments. 4 Chapter 1. Introduction to MongoDB CHAPTER 2 Install MongoDB MongoDB runs on most platforms and supports both 32-bit and 64-bit architectures. 2.1 Recommended Operating Systems for Production Deployments Amazon Linux Debian 7.1 RedHat / CentOS 6.2+ SUSE 11 Ubuntu LTS 12.04 Ubuntu LTS 14.04 Windows Server 2012 MongoDB supported supported supported supported supported supported supported MongoDB Enterprise supported supported supported supported supported supported supported 2.2 Other Supported Operating Systems Mac OSX 10.6+ RedHat / CentOS 5.5+ RedHat / CentOS 5.7+ RedHat / CentOS 7.0+ SmartOS Solaris 11 / SunOS 5.11 on x86 Windows Server 2008 R2 MongoDB supported supported supported supported supported supported supported MongoDB Enterprise supported supported supported 2.3 Installation Guides See the Release Notes (page 769) for information about specific releases of MongoDB. Install on Linux (page 6) Documentations for installing the official MongoDB distribution on Linux-based systems. Install on Red Hat (page 7) Install MongoDB on Red Hat Enterprise and related Linux systems using .rpm packages. Install on Amazon Linux (page 12) Install MongoDB on Amazon Linux systems using .rpm packages. Install on SUSE (page 9) Install MongoDB on SUSE Linux systems using .rpm packages. 5 MongoDB Documentation, Release 3.0.2 Install on Ubuntu (page 14) Install MongoDB on Ubuntu Linux systems using .deb packages. Install on Debian (page 17) Install MongoDB on Debian systems using .deb packages. Install on Other Linux Systems (page 19) Install the official build of MongoDB on other Linux systems from MongoDB archives. Install on OS X (page 21) Install the official build of MongoDB on OS X systems from Homebrew packages or from MongoDB archives. Install on Windows (page 24) Install MongoDB on Windows systems and optionally start MongoDB as a Windows service. Install MongoDB Enterprise (page 29) MongoDB Enterprise is available for MongoDB Enterprise subscribers and includes several additional features including support for SNMP monitoring, LDAP authentication, Kerberos authentication, and System Event Auditing. Install MongoDB Enterprise on Red Hat (page 29) Install the MongoDB Enterprise build and required dependencies on Red Hat Enterprise or CentOS Systems using packages. Install MongoDB Enterprise on Ubuntu (page 32) Install the MongoDB Enterprise build and required dependencies on Ubuntu Linux Systems using packages. Install MongoDB Enterprise on Amazon AMI (page 40) Install the MongoDB Enterprise build and required dependencies on Amazon Linux AMI. Install MongoDB Enterprise on Windows (page 41) Install the MongoDB Enterprise build and required dependencies using the .msi installer. 2.3.1 Install on Linux These documents provide instructions to install MongoDB for various Linux systems. Recommended For the best installation experience, MongoDB provides packages for popular Linux distributions. These packages, which support specific platforms and provide improved performance and SSL support, are the preferred way to run MongoDB. The following guides detail the installation process for these systems: Install on Red Hat (page 7) Install MongoDB on Red Hat Enterprise and related Linux systems using .rpm packages. Install on SUSE (page 9) Install MongoDB on SUSE Linux systems using .rpm packages. Install on Amazon Linux (page 12) Install MongoDB on Amazon Linux systems using .rpm packages. Install on Ubuntu (page 14) Install MongoDB on Ubuntu Linux systems using .deb packages. Install on Debian (page 17) Install MongoDB on Debian systems using .deb packages. For systems without supported packages, refer to the Manual Installation tutorial. Manual Installation For Linux systems without supported packages, MongoDB provides a generic Linux release. These versions of MongoDB don’t include SSL, and may not perform as well as the targeted packages, but are compatible on most contemporary Linux systems. See the following guides for installation: Install on Other Linux Systems (page 19) Install the official build of MongoDB on other Linux systems from MongoDB archives. 6 Chapter 2. Install MongoDB MongoDB Documentation, Release 3.0.2 Install MongoDB on Red Hat Enterprise or CentOS Linux Overview Use this tutorial to install MongoDB on Red Hat Enterprise Linux CentOS Linux using .rpm packages. While some of these distributions include their own MongoDB packages, the official MongoDB packages are generally more up to date. Packages MongoDB provides packages of the officially supported MongoDB builds in its own repository. This repository provides the MongoDB distribution in the following packages: • mongodb-org This package is a metapackage that will automatically install the four component packages listed below. • mongodb-org-server This package contains the mongod daemon and associated configuration and init scripts. • mongodb-org-mongos This package contains the mongos daemon. • mongodb-org-shell This package contains the mongo shell. • mongodb-org-tools This package contains the following MongoDB tools: mongoimport bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop. Control Scripts The mongodb-org package includes various control scripts, including the init script /etc/rc.d/init.d/mongod. These scripts are used to stop, start, and restart daemon processes. The package configures MongoDB using the /etc/mongod.conf file in conjunction with the control scripts. See the Configuration File reference for documentation of settings available in the configuration file. As of version 3.0.2, there are no control scripts for mongos. The mongos process is used only in sharding (page 653). You can use the mongod init script to derive your own mongos control script for use in such environments. See the mongos reference for configuration details. Considerations For production deployments, always run MongoDB on 64-bit systems. The default /etc/mongod.conf configuration file supplied by the 3.0 series packages has bind_ip set to 127.0.0.1 by default. Modify this setting as needed for your environment before initializing a replica set. Changed in version 2.6: The package structure and names have changed as of version 2.6. For instructions on installation of an older release, please refer to the documentation for the appropriate version. Install MongoDB Step 1: Configure the package management system (yum). Create a /etc/yum.repos.d/mongodb-org-3.0.repo file so that you can install MongoDB directly, using yum. Use the following repository file to specify the latest stable release of MongoDB. 2.3. Installation Guides 7 MongoDB Documentation, Release 3.0.2 [mongodb-org-3.0] name=MongoDB Repository baseurl= gpgcheck=0 enabled=1 Use the following repository to install only versions of MongoDB for the 3.0 release. If you’d like to install MongoDB packages from a particular release series (page 899), such as 2.4 or 2.6, you can specify the release series in the repository configuration. For example, to restrict your system to the 2.6 release series, create a /etc/yum.repos.d/mongodb-org-2.6.repo file to hold the following configuration information for the MongoDB 2.6 repository: [mongodb-org-2.6] name=MongoDB 2.6 Repository baseurl= gpgcheck=0 enabled=1 .repo files for each release can also be found in the repository itself1 . Remember that odd-numbered minor release versions (e.g. 2.5) are development versions and are unsuitable for production use. Step 2: Install the MongoDB packages and associated tools. When you install the packages, you choose whether to install the current release or a previous one. This step provides the commands for both. To install the latest stable version of MongoDB, issue the following command: sudo yum install -y mongodb-org To install a specific release of MongoDB, specify each component package individually and append the version number to the package name, as in the following example: sudo yum install -y mongodb-org-3.0.2 mongodb-org-server-3.0.2 mongodb-org-shell-3.0.2 mongodb-org-mo You can specify any available version of MongoDB. However yum will upgrade the packages when a newer version becomes available. To prevent unintended upgrades, pin the package. To pin a package, add the following exclude directive to your /etc/yum.conf file: exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools Versions of the MongoDB packages before 2.6 use a different repo location. Refer to the version of the documentation appropriate for your MongoDB version. Run MongoDB Important: You must configure SELinux to allow MongoDB to start on Red Hat Linux-based systems (Red Hat Enterprise Linux or CentOS Linux). Administrators have three options: • enable access to the relevant ports (e.g. 27017) for SELinux. See Default MongoDB Port (page 416) for more information on MongoDB’s default ports. For default settings, this can be accomplished by running semanage port -a -t mongod_port_t -p tcp 27017 • set SELinux to permissive mode in /etc/selinux.conf. The line SELINUX=enforcing should be changed to 1 {{distro_name}}/ 8 Chapter 2. Install MongoDB MongoDB Documentation, Release 3.0.2 SELINUX=permissive • disable SELinux entirely; as above but set SELINUX=disabled All three options require root privileges. The latter two options each requires a system reboot and may have larger implications for your deployment. You may alternatively choose not to install the SELinux packages when you are installing your Linux operating system, or choose to remove the relevant packages. This option is the most invasive and is not recommended. The MongoDB instance stores its data files in /var/lib/mongo and its log files in /var/log/mongodb by default, and runs using the mongod user account. You can specify alternate log and data file directories in /etc/mongod.conf. See systemLog.path and storage.dbPath for additional information. If you change the user that runs the MongoDB process, you must modify the access control rights to the /var/lib/mongo and /var/log/mongodb directories to give this user access to these directories. Step 1: Start MongoDB. You can start the mongod process by issuing the following command: sudo service mongod start Step 2: Verify that MongoDB has started successfully You can verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading [initandlisten] waiting for connections on port <port> where <port> is the port configured in /etc/mongod.conf, 27017 by default. You can optionally ensure that MongoDB will start following a system reboot by issuing the following command: sudo chkconfig mongod on Step 3: Stop MongoDB. As needed, you can stop the mongod process by issuing the following command: sudo service mongod stop Step 4: Restart MongoDB. You can restart the mongod process by issuing the following command: sudo service mongod restart You can follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log file. Step 5: Begin u...
View Full Document

  • Winter '17
  • jackson
  • Debian, Linux distribution

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture