Skip to content

Latest commit

 

History

History
878 lines (641 loc) · 31 KB

mongodb_handbook.md

File metadata and controls

878 lines (641 loc) · 31 KB

Import the public key used by the package management system.

sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

Create a list file for MongoDB

Create the /etc/apt/sources.list.d/mongodb-org-4.2.list file for Ubuntu 18.04 (Bionic):

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Create the /etc/apt/sources.list.d/mongodb-org-4.2.list file for Ubuntu 16.04 (Xenial):

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Reload local package database.

sudo apt-get update

Install the MongoDB packages

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org

To install a specific release, you must specify each component package individually along with the version number, as in the following example:

sudo apt-get install -y mongodb-org=4.2.8 mongodb-org-server=4.2.8 mongodb-org-shell=4.2.8 mongodb-org-mongos=4.2.8 mongodb-org-tools=4.2.8

To prevent unintended upgrades, you can pin the package at the currently installed version:

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

Init System

To run and manage your mongod process, you will be using your operating system’s built-in init system. Recent versions of Linux tend to use systemd (which uses the systemctl command), while older versions of Linux tend to use System V init (which uses the service command).

If you are unsure which init system your platform uses, run the following command:

ps --no-headers -o comm 1
sudo systemctl start mongod
sudo systemctl status mongod
sudo systemctl enable mongod
sudo systemctl stop mongod
sudo systemctl restart mongod
mongo

Uninstall MongoDB Community Edition

sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
mongo
mongo --port 27017
mongo "mongodb://mongodb0.example.com:28015"
mongo --host mongodb0.example.com:28015
mongo --host mongodb0.example.com --port 28015
mongo "mongodb://alice@mongodb0.examples.com:28015/?authSource=admin"
mongo --username alice --password --authenticationDatabase admin --host mongodb0.examples.com --port 28015
mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
mongo --host replA/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017
mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA&ssl=true"
mongo "mongodb+srv://server.example.com/"
mongo --ssl --host replA/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017

Working with the mongo Shell

mongo --help
db
help
use test
show dbs
quit()

Collection Help

show collections
db.collection.save

Insert a Single Document

db.inventory.insertOne(
   { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
db.inventory.find( { item: "canvas" } )

Insert Multiple Documents

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
   { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
   { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
db.inventory.find( {} )

MongoDB provides the following methods for inserting documents into a collection:

db.collection.insertOne() Inserts a single document into a collection.
db.collection.insertMany() db.collection.insertMany() inserts multiple documents into a collection.
db.collection.insert() db.collection.insert() inserts a single document or multiple documents into a collection.

Additional Methods for Inserts

The following methods can also add new documents to a collection:

Select All Documents in a Collection

db.inventory.find( {} )

Specify Equality Condition

To specify equality conditions, use <field>:<value> expressions in the query filter document:

{ <field1>: <value1>, ... }

The following example selects from the inventory collection all documents where the status equals "D":

db.inventory.find( { status: "D" } )

Specify Conditions Using Query Operators

A query filter document can use the query operators to specify conditions in the following form:

{ <field1>: { <operator1>: <value1> }, ... }

The following example retrieves all documents from the inventory collection where status equals either "A" or "D":

db.inventory.find( { status: { $in: [ "A", "D" ] } } )

Specify AND Conditions

db.inventory.find( { status: "A", qty: { $lt: 30 } } )

Specify OR Conditions

db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )

Specify AND as well as OR Conditions

db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )

NOTE

MongoDB supports regular expressions $regex queries to perform string pattern matches.

Match an Embedded/Nested Document

db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )

Query on Nested Field

To specify a query condition on fields in an embedded/nested document, use dot notation ("field.nestedField").

NOTE When querying using dot notation, the field and nested field must be inside quotation marks.

db.inventory.find( { "size.uom": "in" } )

Specify Match using Query Operator

db.inventory.find( { "size.h": { $lt: 15 } } )

Specify AND Condition

db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

Match an Array

To specify equality condition on an array, use the query document { <field>: <value> } where <value> is the exact array to match, including the order of the elements.

db.inventory.find( { tags: ["red", "blank"] } )
db.inventory.find( { tags: { $all: ["red", "blank"] } } )

Query an Array for an Element

The following example queries for all documents where tags is an array that contains the string "red" as one of its elements:

db.inventory.find( { tags: "red" } )

For example, the following operation queries for all documents where the array dim_cm contains at least one element whose value is greater than 25.

db.inventory.find( { dim_cm: { $gt: 25 } } )

Specify Multiple Conditions for Array Elements

Query an Array with Compound Filter Conditions on the Array Elements

The following example queries for documents where the dim_cm array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both:

db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

Query for an Array Element that Meets Multiple Criteria

Use $elemMatch operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.

The following example queries for documents where the dim_cm array contains at least one element that is both greater than ($gt) 22 and less than ($lt) 30:

db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )

Query for an Element by the Array Index Position

The following example queries for all documents where the second element in the array dim_cm is greater than 25:

db.inventory.find( { "dim_cm.1": { $gt: 25 } } )

Query an Array by Array Length

db.inventory.find( { "tags": { $size: 3 } } )

Start MongoDB without access control.

mongod --port 27017 --dbpath /var/lib/mongodb

Connect to the instance.

mongo --port 27017

Create the user administrator.

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Re-start the MongoDB instance with access control.

sudo vi /etc/mongod.conf

add

security:
    authorization: enabled

Restart

sudo systemctl restart mongod

Or

db.adminCommand( { shutdown: 1 } )
mongod --auth --port 27017 --dbpath /var/lib/mongodb

Connect and authenticate as the user administrator.

mongo --port 27017
use admin
db.auth("myUserAdmin", passwordPrompt()) // or cleartext password
mongo --port 27017  --authenticationDatabase "admin" -u "myUserAdmin" -p

Create additional users as needed for your deployment.

use test
db.createUser(
  {
    user: "myTester",
    pwd:  passwordPrompt(),   // or cleartext password
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)

Connect to the instance and authenticate as myTester.

mongo --port 27017 -u "myTester" --authenticationDatabase "test" -p

or

mongo --port 27017
use test
db.auth("myTester", passwordPrompt())  // or cleartext password

Create a Role to Manage Current Operations

Connect to MongoDB with the appropriate privileges.

mongo --port 27017 -u myUserAdmin -p 'abc123' --authenticationDatabase 'admin'

Create a new role to manage current operations.

use admin
db.createRole(
   {
     role: "manageOpRole",
     privileges: [
       { resource: { cluster: true }, actions: [ "killop", "inprog" ] },
       { resource: { db: "", collection: "" }, actions: [ "killCursors" ] }
     ],
     roles: []
   }
)

Create a Role to Run mongostat

Connect to MongoDB with the appropriate privileges.

mongo --port 27017 -u myUserAdmin -p 'abc123' --authenticationDatabase 'admin'

Create a new role to manage current operations.

use admin
db.createRole(
   {
     role: "mongostatRole",
     privileges: [
       { resource: { cluster: true }, actions: [ "serverStatus" ] }
     ],
     roles: []
   }
)

Create a Role to Drop system.views Collection across Databases

Connect to MongoDB with the appropriate privileges.

mongo --port 27017 -u myUserAdmin -p 'abc123' --authenticationDatabase 'admin'

Create a new role to drop the system.views collection in any database.

use admin
db.createRole(
   {
     role: "dropSystemViewsAnyDatabase",
     privileges: [
       {
         actions: [ "dropCollection" ],
         resource: { db: "", collection: "system.views" }
       }
     ],
     roles: []
   }
)

Modify Access for an Existing User

Connect to MongoDB with the appropriate privileges.

mongo --port 27017 -u myUserAdmin -p 'abc123' --authenticationDatabase 'admin'

Identify the user’s roles and privileges.

use reporting
db.getUser("reportsUser")

To display the privileges granted to the user by the readWrite role on the "accounts" database, issue:

use accounts
db.getRole( "readWrite", { showPrivileges: true } )

Identify the privileges to grant or revoke.

Modify the user’s access.

Revoke a Role

use reporting
db.revokeRolesFromUser(
    "reportsUser",
    [
      { role: "readWrite", db: "accounts" }
    ]
)

Grant a Role

use reporting
db.grantRolesToUser(
    "reportsUser",
    [
      { role: "read", db: "accounts" }
    ]
)

Modify the Password for an Existing User

Connect to MongoDB with the appropriate privileges.

mongo --port 27017 -u myUserAdmin -p 'abc123' --authenticationDatabase 'admin'

Change the password.

db.changeUserPassword("reporting", "SOh3TbYhxuLiW8ypJPxmt1oOfL")

View a User’s Roles

use reporting
db.getUser("reportsUser")

View a Role’s Privileges

use products
db.getRole( "read", { showPrivileges: true } )
sudo vi /etc/mongod.conf

update bindIp

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

restart service

sudo systemctl restart mongod
rs.initiate( {
   _id : "rs0",
   members: [
      { _id: 0, host: "mongodb0.example.net:27017" },
      { _id: 1, host: "mongodb1.example.net:27017" },
      { _id: 2, host: "mongodb2.example.net:27017" }
   ]
})
rs.conf()
rs.status()

On slave

rs.slaveOk()
show collections
db.printSlaveReplicationInfo()
rs.add('10.20.20.5:27017')
rs.add({"_id":3,"host":"10.20.20.5:27017","priority":0,"hidden":true}
rs.add({"_id":3,"host":"10.20.20.5:27017","priority":0,"slaveDelay":60})
rs.remove('10.20.20.5:27017')
rs.addArb('192.168.168.131:27017')
rs.add({"_id":3,"host":"10.20.20.5:27017","arbiterOnly":true})

On Primary

use admin
db.shutdownServer()
sudo mkdir -p /data/mongodb/data/configServer
sudo mkdir -p /data/mongodb/data/shard1
sudo mkdir -p /data/mongodb/data/shard2
sudo mkdir -p /data/mongodb/data/shard3
sudo mkdir -p /data/mongodb/log
sudo mkdir -p /data/mongodb/pid

Create the Config Server Replica Set

If using a configuration file, set:

sharding:
  clusterRole: configsvr
replication:
  replSetName: <replica set name>
net:
  bindIp: localhost,<hostname(s)|ip address(es)>

If using the command line options, start the mongod with the --configsvr, --replSet, --bind_ip, and other options as appropriate to your deployment. For example:

sudo mongod --configsvr --replSet rscs --dbpath /data/mongodb/data/configServer --bind_ip 0.0.0.0

Connect to one of the config servers.

mongo --port 27019

Initiate the replica set.

rs.initiate(
  {
    _id: "rscs",
    configsvr: true,
    members: [
      { _id : 0, host : "server1:27019" },
      { _id : 1, host : "server2:27019" },
      { _id : 2, host : "server3:27019" }
    ]
  }
)

Create the Shard Replica Sets

Start each member of the shard replica set.

If using a configuration file, set:

sharding:
    clusterRole: shardsvr
replication:
    replSetName: <replSetName>
net:
    bindIp: localhost,<ip address>

If using the command line option, start the mongod with the --replSet, and --shardsvr, --bind_ip options, and other options as appropriate to your deployment. For example:

mongod --shardsvr --replSet <replSetname>  --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>

For example:

sudo mongod --shardsvr --replSet rsss --dbpath /data/mongodb/data/shard1 --bind_ip 0.0.0.0
sudo mongod --shardsvr --replSet rsss2 --dbpath /data/mongodb/data/shard2 --bind_ip 0.0.0.0  --port 27028
sudo mongod --shardsvr --replSet rsss3 --dbpath /data/mongodb/data/shard3 --bind_ip 0.0.0.0  --port 27038

Connect to one member of the shard replica set.

Initiate the replica set.

mongo --port 27018
rs.initiate(
  {
    _id: "rsss",
    members: [
      { _id : 0, host : "server1:27018" },
      { _id : 1, host : "server2:27018" },
      { _id : 2, host : "server3:27018" }
    ]
  }
)
mongo --port 27028
rs.initiate(
  {
    _id: "rsss2",
    members: [
      { _id : 0, host : "server1:27028" },
      { _id : 1, host : "server2:27028" },
      { _id : 2, host : "server3:27028" }
    ]
  }
)
mongo --port 27038
rs.initiate(
  {
    _id: "rsss3",
    members: [
      { _id : 0, host : "server1:27038" },
      { _id : 1, host : "server2:27038" },
      { _id : 2, host : "server3:27038" }
    ]
  }
)

Start a mongos for the Sharded Cluster

If using a configuration file, set the sharding.configDB to the config server replica set name and at least one member of the replica set in <replSetName>/<host:port> format.

sharding:
  configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019
net:
  bindIp: localhost,<hostname(s)|ip address(es)>

If using command line parameters start the mongos and specify the --configdb, --bind_ip, and other options as appropriate to your deployment. For example:

mongos --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,cfg3.example.net:27019 --bind_ip localhost,<hostname(s)|ip address(es)>

For example:

sudo mongos --configdb rscs/server1:27019,server2:27019,server3:27019 --bind_ip 0.0.0.0 --port 27016

Connect to the Sharded Cluster

mongo --port 27016

Add Shards to the Cluster

sh.addShard( "rsss/server1:27018,server2:27018,server3:27018")
sh.addShard( "rsss2/server1:27028,server2:27028,server3:27028")
sh.addShard( "rsss3/server1:27038,server2:27038,server3:27038")

Enable Sharding for a Database

sh.enableSharding("test")

Shard a Collection

sh.shardCollection("test.testshard", { item : "hashed" } )
db.testshard.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);
db.testshard.stats()

mongodump only captures the documents in the database. The resulting backup is space efficient, but mongorestore or mongod must rebuild the indexes after restoring data.

When you run mongodump without any arguments, the command connects to the MongoDB instance on the local system (e.g. localhost) on port 27017 and creates a database backup named dump/ in the current directory.

mongodump
mongodump --oplog --out=/data/backup/
mongodump --host=mongodb.example.net --port=27017
mongodump --out=/data/backup/
mongodump --collection=myCollection --db=test
mongodump --host=mongodb1.example.net --port=3017 --username=user --password="pass" --out=/opt/backup/mongodump-2013-10-24
mongorestore --port=<port number> <path to the backup>
mongorestore dump-2013-10-25/
mongorestore --oplogReplay
mongorestore --host=mongodb1.example.net --port=3017
mongorestore --host=mongodb1.example.net --port=3017 --username=user  --authenticationDatabase=admin /opt/backup/mongodump-2013-10-24
use accounts
db.getUser("appClient")
db.getUsers()
db.getUsers({ filter: { mechanisms: "SCRAM-SHA-256" } })