center

Documentations and Articles

Repositories

  • awesome-mongodb: πŸƒ A curated list of awesome MongoDB resources, libraries, tools and applications

High-Availability & Scaling

Tips & Configuration

Optimization

Monitoring and Observability

Security

General

MongoDB Tools

center

Backup

  • percona-backup-mongodb: a distributed, low-impact solution for achieving consistent backups of MongoDB sharded clusters and replica sets

Driver & Connector

  • mongobetween: a proxying connections for mongodb. It is developed by coinbase.

MongoDB Installation

To installation MongoDB into your host in binary package, mongod is one of things you should setup in your host. By default, your host will not find any package related to mongodb, especially Ubuntu, so we need add keymap and multiple things to help apt package can find and download them for us

Like as another tools, you have repository to find the key to let your apt know what version and repository to download package, and mongodb does the same things. Explore more about at

# For example, we will download the mongodb latest version: 8.0
# Download requirement package and import the public key
sudo apt install -y gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
   --dearmor
   
# Add the file list to help apt find the mongodb repository
. /etc/os-release
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu $VERSION_CODENAME/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
 
# Reload package database
sudo apt update
 
# find and install mongodb community server
sudo apt search mongodb-org
sudo apt install -y mongodb-org
 
# To specific minor version, you can use
# sudo apt install -y mongodb-org=8.0.3
 
# When you want to downgrade version, perform with --allow-downgrades
# sudo apt install mongodb-org=8.0.3 --allow-downgrades -y

By the way to look more detail and try by your own, please double-check the MongoDB - Install MongoDB Community Edition on Ubuntu

If you consider to find the alternative way and more solution, these below resources can be help you couple of things

Install MongoDB Tools

Mongoshell

You can download and install mongosh. If you already use linux, you can follow my guideline

wget https://downloads.mongodb.com/compass/mongodb-mongosh_2.4.2_amd64.deb
chmod +x  mongodb-mongosh_2.4.2_amd64.deb
sudo dpkg -i mongodb-mongosh_2.4.2_amd64.deb

After download, you can validate the mongosh by

mongosh --version

Backup and Restore

To backup and restore, we need to use MongoDB Database Tools, such as mongodump and mongorestore. Following the guideline to install

wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian10-x86_64-100.12.0.deb
chmod +x mongodb-database-tools-debian10-x86_64-100.12.0.deb
sudo dpkg -i mongodb-database-tools-debian10-x86_64-100.12.0.deb

Login into cluster

# Connect via parameter
mongosh --host <host> --port <port> -u <username> -p <password>
 
# Connect via connection string
mongosh 'mongodb://<user>:<pass>@<host>:<port>/<collection>'

Knowledge

Use MongoDB Connector with DirectConnection

This configuration relate in configuration of MongoDB, when you set replicaset mode for your MongoDB, when you double-check the configuration member, you can see this at

For the configuration, you can use command with mongosh

rs.conf()

It will show your replicaset configuration in MongoDB, like this

{
  _id: <string>,
  version: <int>,
  term: <int>,
  protocolVersion: <number>,
  writeConcernMajorityJournalDefault: <boolean>,
  configsvr: <boolean>,
  members: [
    {
      _id: <int>,
      host: <string>,
      arbiterOnly: <boolean>,
      buildIndexes: <boolean>,
      hidden: <boolean>,
      priority: <number>,
      tags: <document>,
      secondaryDelaySecs: <int>,
      votes: <number>
    },
    ...
  ],
  settings: {
    chainingAllowed : <boolean>,
    heartbeatIntervalMillis : <int>,
    heartbeatTimeoutSecs: <int>,
    electionTimeoutMillis : <int>,
    catchUpTimeoutMillis : <int>,
    getLastErrorModes : <document>,
    getLastErrorDefaults : <document>,
    replicaSetId: <ObjectId>
  }
}

If It returns your host configuration as private ip address, you can encounter some problems about connection when this IP range same as IP range used for your network, that why sometime it will show connection timeout. So for fix this problem, you should connect directly into replicaset via directConnection=true parameter

mongosh 'mongodb://username:password@server-uri/db?directConnection=true'

Info

In some situations, you can bypass this configuration because it will add default into your connection string, like

  • TheΒ replicaSetΒ query parameter is present in the connection string.
  • The connection string uses theΒ mongodb+srv://Β connection string format.
  • The connection string contains a seed list with multiple hosts.
  • The connection string already contains aΒ directConnectionΒ parameter.

Backup and Restore

By default, MongoDB provide built-in tools for dump and restore database and serve them for backup and restore progress. It’s simple way to execute these actions and in-directly, explore more at MongoDB - Back Up and Restore a Self-Managed Deployment with MongoDB Tools

However, you have alternative solutions for handling this stuff and you can choose them, it depends on you

Backup

For backup, I usually and almost use mongodump to handle backup (logical), it lets you combine with multiple optional, compress and sent them via flexible output

Warning

When you backup for huge database or collection, you need to consider to choose this mongodump solution which load a huge data to memory and cause crash your hosting, especially with mongodb core wiredtiger and Medium - Solving curious case of excess memory consumption by MongoDB

# basic mongodump command
mongodump <options> <connection-string>
 
# usecase: backup specific database/ w collection
mongodump --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --db $DB_NAME [--collection name] --out $BACKUP_DIR
 
# usecase: gzip compress
mongodump --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --db $DB_NAME --out $BACKUP_DIR --gzip
 
# usecase: for point-in-time recovery, you should add --oplog when backup whole database
mongodump --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --db $DB_NAME --out $BACKUP_DIR --oplog
 
# usecase: optimize backup with read node in replicaSet
# preference: https://www.mongodb.com/docs/manual/core/read-preference/#read-preference-modes
mongodump --uri 'mongodb://user:pass@host1:port1,host2:port2,.../admin?replicaSet=replicaset_name'--readPreference 'secondary'
 
# usecase: optimize backup with --numParallelCollections or -j for reducing the pressure memory for backup
mongodump --uri 'mongodb://user:pass@host1:port1,host2:port2,.../admin?replicaSet=replicaset_name' --numParallelCollections num-threads

Restore

For restore, I recommend to use mongorestore to get the compatible with your backup version by mongodump

# basic mongorestore command
mongorestore <options> <connection-string> <directory or file to restore>
 
# usecase: basic usage
mongorestore --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --db $DB_NAME /path/to/backup
 
# usecase: drop database and skip create index before restore
mongorestore --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --db $DB_NAME --drop --noIndexRestore /path/to/backup
 
# usecase: restore with gzip decompress
mongorestore --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --db $DB_NAME --drop --noIndexRestore --gzip /path/to/backup

Backup Script for MongoDB Cluster

#!/bin/bash
 
# MongoDB connection information
MONGO_HOST="MONGO_HOST"
MONGO_PORT="MONGO_PORT"
MONGO_USER="MONGO_USER"
MONGO_PASSWORD="MONGO_PASSWORD"
 
# MinIO connection information
MINIO_ALIAS="MINIO_ALIAS"
MINIO_BUCKET="MINIO_BUCKET"
 
# Get a list of all databases
DATABASES=$(mongosh --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --quiet --eval "db.adminCommand('listDatabases').databases.map(db => db.name).join(' ')")
 
# Backup each database
for DB_NAME in $DATABASES; do
    BACKUP_DIR="${DB_NAME}_backup"
    
    echo "Backing up database: $DB_NAME"
    mongodump --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --db $DB_NAME --out $BACKUP_DIR
    
    # Create a tar.gz archive of the backup
    tar -czvf $BACKUP_DIR.tar.gz $BACKUP_DIR
    
    # Upload to MinIO
    echo "Uploading $BACKUP_DIR.tar.gz to MinIO"
    mc cp $BACKUP_DIR.tar.gz $MINIO_ALIAS/$MINIO_BUCKET/$BACKUP_DIR.tar.gz
    
    # Optionally remove the local backup directory and archive
    rm -rf $BACKUP_DIR
    rm $BACKUP_DIR.tar.gz
done
 
echo "All databases backed up and uploaded to MinIO."

Roles in MongoDB

When you enable authentication in MongoDB, any client connection will be required to submit a valid username and password corresponding to a defined role.

The following is a list of small, but important, built-in roles you can define in MongoDB:

  • read: Grants permissions to perform read-only operations on a specified database.
  • readWrite: Grants permissions for both read and write operations on a specified database.
  • dbAdmin: Grants administrative privileges over a specified database, including actions like managing indexes, running database commands, and managing database statistics.
  • userAdmin: Grants privileges to create and manage users and roles on a specified database.
  • dbOwner: A high-level role that grants all administrative, read, and write access on a specified database.

To explore the full range of roles and permissions, refer to the official documentation: MongoDB - Built-In Roles 🌟 (Recommended)

# change your account into admin
use admin
 
# create a userAdmin, who carry on about setting user/passwd and authentication in any database
db.createUser(
   {
     user: "mongo-useradmin",
     pwd: passwordPrompt(),
     roles: [
        { role: "userAdminAnyDatabase", db: "admin" }
     ]
   }
)
 
# create a root, who carry on about whole configuration, useradmin, readwrite and authentication in any database
db.createUser(
   {
     user: "mongo-root",
     pwd: passwordPrompt(),
     roles: [
        { role: "root", db: "admin" }
     ]
   }
)
 
# create a readwrite, who carry on about whole readwrite in any database
db.createUser(
   {
     user: "mongo-readwrite",
     pwd: passwordPrompt(),
     roles: [
        { role: "readwrite", db: "admin" }
     ]
   }
)

Common Query Configuration of MongoDB

When you work with MongoDB, there are sort of queries for checking the configuration, such as: replicaSet, isMaster, Opslog, …

Get the OpsLog information

To double-check the opslog in replication in current state and default configuration, you can use

> rs.printReplicationInfo()
 
actual oplog size
'9184.92578125 MB'
---
configured oplog size
'9184.92578125 MB'
---
log length start to end
'2565292 secs (712.58 hrs)'
---
oplog first event time
'Sun Aug 17 2025 22:47:34 GMT+0700 (Indochina Time)'
---
oplog last event time
'Tue Sep 16 2025 15:22:26 GMT+0700 (Indochina Time)'
---
now
'Tue Sep 16 2025 15:22:32 GMT+0700 (Indochina Time)'

Get the replicaSet configuration

In the situation, you want to see what replicaset in configuration, you can use

# Get configuration of replicaset
> rs.conf()
 
{
  _id: 'rs0',
  version: 3,
  term: 2,
  members: [
    {
      _id: 0,
      host: 'x.x.x.x:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    }
  ],
  protocolVersion: Long('1'),
  writeConcernMajorityJournalDefault: true,
  settings: {
    chainingAllowed: true,
    heartbeatIntervalMillis: 2000,
    heartbeatTimeoutSecs: 10,
    electionTimeoutMillis: 10000,
    catchUpTimeoutMillis: -1,
    catchUpTakeoverDelayMillis: 30000,
    getLastErrorModes: {},
    getLastErrorDefaults: { w: 1, wtimeout: 0 },
    replicaSetId: ObjectId('xxxxxxxxxxx')
  }
}
 
# Get status of replicaset
> rs.status()

Check your host is Primary Or Secondary Or Arbiter

If you operate your MongoDB in replicaSet architecture, you will approach there type of node in MongoDB

  • Primary: the only member in the replica set that receives write operations. MongoDB applies write operations on theΒ primaryΒ and then records the operations on the primary’sΒ oplog
  • Secondary: A secondary maintains a copy of theΒ primary’sΒ data set. To replicate data, a secondary applies operations from the primary’sΒ oplogΒ to its own data set in an asynchronous process.Β A replica set can have one or more secondaries
  • Arbiter: A participant participates inΒ elections for primaryΒ but an arbiter doesΒ notΒ have a copy of the data set andΒ cannotΒ become a primary

You can use bunch of queries below to find what role of your mongodb

> rs.isMaster()
 
{
  topologyVersion: {
    processId: ObjectId('xxxxxxxxxxx'),
    counter: Long('6')
  },
  hosts: [ 'x.x.x.x:27017' ],
  setName: 'rs0',
  setVersion: 3,
  ismaster: true,
  secondary: false,
  primary: 'x.x.x.x:27017',
  me: 'x.x.x.x:27017',
  electionId: ObjectId('7fffffff0000000000000002'),
  lastWrite: {
    opTime: { ts: Timestamp({ t: 1758014134, i: 11 }), t: Long('2') },
    lastWriteDate: ISODate('2025-09-16T09:15:34.000Z'),
    majorityOpTime: { ts: Timestamp({ t: 1758014134, i: 11 }), t: Long('2') },
    majorityWriteDate: ISODate('2025-09-16T09:15:34.000Z')
  },
  maxBsonObjectSize: 16777216,
  maxMessageSizeBytes: 48000000,
  maxWriteBatchSize: 100000,
  localTime: ISODate('2025-09-16T09:15:36.923Z'),
  logicalSessionTimeoutMinutes: 30,
  connectionId: 1140819,
  minWireVersion: 0,
  maxWireVersion: 21,
  readOnly: false,
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1758014134, i: 13 }),
    signature: {
      hash: Binary.createFromBase64('xxxxxxxxxxxxxxx', 0),
      keyId: Long('xxxxxxxxxxxxxx')
    }
  },
  operationTime: Timestamp({ t: 1758014134, i: 11 }),
  isWritablePrimary: true
}