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
Resources: presentations, webinars, white papers
Key features
Flexible schema
Performance
High Availability
Primary / Secondaries architecture
BSON storage (Binary JSON)
GeoJSON Objects support of GeoJson format for encoding a variety of geographic data structures
ACID transactions
Replication
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
Write concern
Read concern
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
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)
First commands
The following command must return a valid output
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
).You can then connect with the MongoDB Shell:
Docker
Check the images already downloaded locally
Get the image for a specific version of MongoDB
Start the container
Server
Start with Docker
Start from the command line
Client
Command line
→ 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
Sample data
Zip code
Download the zip file export from docs.mongodb.com/manual/tutorial/aggregation-zip-code-data-set.
Import the data into your MongoDB server
You can also import the data to your Atlas cluster
dbKoda samples
dbKoda holds a collection of sample data: github.com/SouthbankSoftware/dbkoda-data.
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.
Deployment typologies
MongoDB & OVH
Last updated