MongoDB

MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era.

→ mongodb.com, Github, developer.mongodb.com

Learn

Official resources

Key features

Replication

Manual

A replica set is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability.

A node of the replica set can be: Primary, Secondary, Arbitrer.

Read preference

Manual

Write concern

Manual

Read concern

Manual

Indexing

Manual Query Plans Limits Analyze Query Performance

MongoDB indexes use a B-tree data structure.

Indexes gets have better read time but have an impact on write time.

Index Types:

  • Single Field

  • Compound Index

  • Multikey Index

  • Geospatial Index

  • Text Indexes

  • Hashed Indexes

Index Properties:

  • Unique Indexes

  • Partial Indexes

  • Sparse Indexes

  • TTL Indexes (Time To Live)

See also Performance Best Practices: Indexing - February 12, 2020

Performance

Manual

Storage engine

The storage engine that is used can be seen with the command db.serverStatus(). It is a mongod option: --storageEngine.

In March 2015, there were two choices: MMAPv1 (original) and WiredTiger (new).

Wired Tiger is new in MongoDB 3.0. It is the first pluggable storage engine.

Features:

  • Document level locking

  • Compression

    • Snappy (default) - fast

    • Zlib - more compression

    • None

  • Lacks some pitfalls of MMAPv1

  • Performance gains

Background:

  • Built separately from MongoDB

  • Used by other's DB

  • Open source

Internals:

  • Stores data in btrees

  • Writes are initially separate, incorporated later

  • Two caches

    • WT caches - 1/2 of RAM (default)

    • FS cache

  • Checkpoint: every minute or more

  • No need for a journal

Transactions

High availability

Data types

Quick Start: BSON Data Types - ObjectId

Products

Getting further

Recipes

Releases

First steps

MongoDB Shell

Go to the download center, select "Server", then "MongoDB Community Server" edition, chose the target platform and version and let the download complete.

MongoDB Shell on Windows

  • You'll download a file like mongodb-win32-x86_64-2008plus-ssl-4.0.4.zip

  • Unzip the content of the archive in a program folder (for example D:\Programs folder)

  • Rename the folder with something explicit like mongodb-community-4.0.4

  • You can either update your PATH globally on your machine or do it when you need it (or through a bat file)

    SET PATH=%PATH%;D:\Programs\mongodb-community-4.0.4\bin

First commands

  • The following command must return a valid output

    mongo --version

    MongoDB shell version v4.0.4 git version: f288a3bdf201007f3693c58e140056adf8b04839 allocator: tcmalloc modules: none build environment: distmod: 2008plus-ssl distarch: x86_64 target_arch: x86_64

Local Server

  • If you followed the steps to have the Mongo Shell, you'll be able to launch easily a MongoDB server locally (mongod).

    # make sure the data path exists
    md /path/to/data
    # start a basic MongoDB instance (default port 27017)
    mongod --dbpath=/path/to/data
  • You can then connect with the MongoDB Shell:

    mongo

Docker

  • Check the images already downloaded locally

    docker images
  • Get the image for a specific version of MongoDB

    docker image pull mongo:4.0.4
  • Start the container

    docker run -d -p 27017:27017 --name mongodb404 mongo:4.0.4

Server

Start with Docker

docker run --name mongodb -d -p 27017:27017 mongo:4.4.6

Start from the command line

mongod --dbpath "C:\my\path" --port 27017

Client

Command line

# start a mongo shell and be on mycollection
mongo --port 27017 mycollection

# restore from dump folder into mydbname database
mongorestore -d mydbname dump

# monitor basic usage statistics for each collection
mongotop

# monitor basic MongoDB server statistics
mongostat

→ docs.mongodb.com/program/mongo

MongoDB Shell (mongosh)

Introduced in June 2020, avalable as a standalone package, it provides a fully functional JavaScript/Node.js environment for interacting with MongoDB deployments. It can be used to test queries and operations directly with one database.

→ Documentation, Download, GitHub, Introduction

mongosh <connection_string>

Sample data

Zip code

  • Import the data into your MongoDB server

    # to be run in the folder containing the json file
    mongoimport --db demoZip --collection zips --file zips.json
    # it should generate the following output
    # 2018-11-19T14:48:53.296+0100    connected to: localhost
    # 2018-11-19T14:48:53.705+0100    imported 29353 documents
  • You can also import the data to your Atlas cluster

    mongoimport --uri "mongodb+srv://user:password@mycluster.mongodb.net/demoZip" --collection zips --file zips.json

dbKoda samples

Open source tools

mtools

mtools is a collection of helper scripts to parse, filter, and visualize MongoDB log files (mongod, mongos). mtools also includes mlaunch, a utility to quickly set up complex MongoDB test environments on a local machine.

More information on github.com/rueckstiess/mtools, mongodb.com/blog/post/introducing-mtools.

You'll need Python (2 or 3) to install and use it.

# install with pip (Python)
pip install mtools

Deployment typologies

MongoDB & OVH

Last updated