Skip to content

MongoDB

May 22, 2025
December 17, 2014

MongoDB after 3.6 uses SSPL

ferretdb

MongoDB: The World’s Leading Modern Database | MongoDB
MongoDB Evolved – Version History | MongoDB
MongoDB 8.0 Is Available Now | MongoDB
MongoDB 6.0 Brings Encrypted Queries, Time-Series Data Collection – The New Stack

Manual Reference - Database Manual v8.0 - MongoDB Docs
What is MongoDB? - Database Manual v8.0 - MongoDB Docs
MongoDB - Back to Basics - YouTube

MongoDB Courses and Trainings | MongoDB University
MongoDB Tutorials | Studio 3T
MongoDB Tutorial for Beginners - YouTube
Manipulating Data With MongoDB. Learn the basics of CRUD with PyMongo | by Benedict Soh | Towards Data Science

How To Manage Data with MongoDB | DigitalOcean
Stock Price Notifications with Mongoose and MongoDB Change Streams | www.thecodebarbarian.com

karlseguin/the-little-mongodb-book: The Little MongoDB Book 2.6

Simple Steps to Optimize Your App Performance with MongoDB, Redis, and Node.js - By
Performance Best Practices: Hardware and OS Configuration | MongoDB

Percona Server for MongoDB

MongoDB Runs Better with Percona
Percona Server for MongoDB

mongod server

mongod - Database Manual v8.0 - MongoDB Docs
Authentication on Self-Managed Deployments - Database Manual v8.0 - MongoDB Docs
Role-Based Access Control in Self-Managed Deployments - Database Manual v8.0 - MongoDB Docs

Booting a mongoDB container with user specified credentials | Lakshmi Narasimhan

docker-library/mongo: Docker Official Image packaging for MongoDB
docs/README.md at master · docker-library/docs

docker-entrypoint-initdb.d, mongo client script
docs/mongo at master · docker-library/docs · GitHub
Initializing mongo db in docker-compose with init script · GitHub

mongo client (mongosh)

mongo — MongoDB Manual
mongo Shell Quick Reference — MongoDB Manual
mongo Shell Methods — MongoDB Manual

MongoDB CRUD Operations — MongoDB Manual
Operators — MongoDB Manual

mongodb-labs/mongosh-snippets: An experimental plugin feature for mongosh

mongosh --host host --port port -u database

# show databases
> show dbs
# use a database
> use <db_name>

# show collections
> show collections

# operations
> db.<collection>.<operation>(<options>)
# query
> db.foo.find({name: 'bar'})
# show schema
> db.foo.findOne()

# drop database
> use db1
switched to db db1
> db.dropDatabase()
{ "dropped" : "db1", "ok" : 1 }
> show dbs
local   0.078125GB
sessions        0.203125GB

Using mongo in shell script:

mongo "$rootAuthDatabase" <<-EOJS
                db.createUser({
                    user: $(_js_escape "$MONGO_INITDB_ROOT_USERNAME"),
                    pwd: $(_js_escape "$MONGO_INITDB_ROOT_PASSWORD"),
                    roles: [ { role: 'root', db: $(_js_escape "$rootAuthDatabase") } ]
                })
            EOJS

mongodump/mongorestore

mongodump - Database Tools - MongoDB Docs
mongorestore - Database Tools - MongoDB Docs

mongodump --archive="mongodump-test-db" --db=test
# with database name change
mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'

mongoexport/mongoimport

mongoexport - Database Tools - MongoDB Docs
mongoimport - Database Tools - MongoDB Docs

mongoexport --db test --collection traffic --out traffic.json
mongoexport --db users --collection contacts --type=csv --fields name,address --out /opt/backups/contacts.csv
# fieldFile can be used for CSV
mongoimport --db test --collection traffic traffic.json
# the CSV has header line
mongoexport --db users --collection contacts --type=csv --headerline contacts.csv

Admin UI

mongo-express - Docker Hub
mongo-express/mongo-express: Web-based MongoDB admin interface, written with Node.js and express
Too fast, too insecure: Securing Mongo Express web administrative interfaces - Help Net Security

On the contrary

Why You Should Never Use MongoDB « Sarah Mei
Why you should never, ever, ever use MongoDB - joepie91's Ramblings
MongoDB queries don’t always return all matching documents! — Meteor Engineering

#perfmatters

Comparing MongoDB performance on public clouds: AWS, Azure & DigitalOcean

Interpret Explain Plan Results - Database Manual v8.0 - MongoDB Docs
Query Plans - Database Manual v8.0 - MongoDB Docs

Partition-Based Sync - Atlas App Services - MongoDB Docs
Partition-Based Sync - Atlas App Services - MongoDB Docs Partition Strategies

Internals

MongoDB internal Architecture. I’m a big believer that database… | by Hussein Nasser | Medium
MongoDB Internal Architecture - YouTube âť—!important

2x Faster Reads and Writes with this MongoDB feature | Clustered Collections - YouTube
Clustered Collections — MongoDB Manual

Previously, a find by _id is two index search, one in _id index for the recordId, one in the internal recordId index for the actual BSON document
Clustered Collections in 5.3 merged the two index and _id index's leaves are the BSON documents

Indexes

Indexing Strategies - Database Manual v8.0 - MongoDB Docs
How To Use Indexes in MongoDB | DigitalOcean

Indexes - Database Manual v8.0 - MongoDB Docs
Compound Indexes - Database Manual v8.0 - MongoDB Docs
Multikey Indexes - Database Manual v8.0 - MongoDB Docs for arrays in documents
Hashed Indexes - Database Manual v8.0 - MongoDB Docs when index field > 1024 bytes
db.collection.createIndex() - Database Manual v8.0 - MongoDB Docs

[SERVER-3294] Ability to keep data on disk in ~ index order - MongoDB Jira WiredTiger does not implement (and cannot guarantee) index clustering on disk level

Create a text index, the use collection.find( { $text: { $search: value } } )

Perform a Text Search (Self-Managed Deployments) - Database Manual v8.0 - MongoDB Docs
How To Perform Full-text Search in MongoDB | DigitalOcean

mongodb - Text search query for text "other" always returns no results? - Stack Overflow stemmed word match, non-language words must be exact match

stop words are by default skipped, use {"language": "none"} when creating index to override this behavior
mongo/src/mongo/db/fts at v4.4 · mongodb/mongo · GitHub

$regex - Database Manual v8.0 - MongoDB Docs
db.collection.find( { field: { $regex: query, $options: "i" } } )

TTL/Auto expiry

Create index on a date field with expireAfterSeconds option

db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 60 }); // auto field (Mongoose)
db.collection.createIndex({ expireAt: 1 }, { expireAfterSeconds: 0 }); // manual field

Using manual field you can change the expire period without recreating the index.

TTL Indexes - Database Manual v8.0 - MongoDB Docs
Expire Data from Collections by Setting TTL - Database Manual v8.0 - MongoDB Docs
How to Erase Expired Docs Automatically with MongoDB (TTL index) | Hacker Noon
MongoDB Auto Expire Documents - DEV Community

Sessions API/transactions

Since 3.6, provides causal consistency, multi-document transactions

MongoDB’s ACID Transaction Guarantee | MongoDB
Transactions - Database Manual - MongoDB Docs
Session - Database Manual v8.0 - MongoDB Docs

A Node.js Perspective on MongoDB 4.0: Transactions | www.thecodebarbarian.com

Mongoose: Transactions

Schema Validation

Since 3.6

Schema Validation - Database Manual - MongoDB Docs
$jsonSchema - Database Manual - MongoDB Docs
How To Use Schema Validation in MongoDB | DigitalOcean

serialization#JSON schema

Showing validation rules:

db.getCollectionInfos({name: "myCollection"})

Mongoose

Mongoose ODM client side ORM and validation
Introduction to Mongoose for MongoDB

Mongoose Schemas
Mongoose: Schema schema method, pre/post hooks
Mongoose: Model
Mongoose: Document

yoitsro/joigoose: Joi validation for your Mongoose models without the hassle of maintaining two schemas Joi -> Mongoose

const joigoose = require("joigoose")(mongoose);

const mongooseSchema = new mongoose.Schema(joigoose.convert(joiSchema));
mongose.model("Model", mongooseSchema);

Search Results | Snyk JSON Schema -> Mongoose
convert-json-schema-to-mongoose - npm package | Snyk
@simplyhexagonal/json-schema-to-mongoose-schema - npm package | Snyk

node.js - MongoDB: output 'id' instead of '_id' - Stack Overflow

const reshapingOptions = {
  // include .id (it's a virtual)
  virtuals: true,

  // exclude .__v
  versionKey: false,

  // exclude ._id
  transform: function (doc, ret) {
    delete ret._id;
    return ret;
  },
};

const friendSchema = mongoose.Schema(
  {
    givenName: String,
    familyName: String,
  },
  { toJSON: reshapingOptions },
);

const friendModel = mongoose.model("Friend", friendSchema);

const john = friendModel.findOne({ givenName: "John" });
if (!john) {
  res.status(404).json({ error: "No John Found" });
}

What is the difference between Mongoose toObject and toJSON? - Stack Overflow
Documentation: clarify the difference between toObject() and toJSON() · Issue #2072 · Automattic/mongoose
Mongoose toObject and toJSON transform behavior with sub-documents | Alexander Zeitler

Python

mongodb/motor: Motor - the async Python driver for MongoDB and Tornado or asyncio
art049/odmantic: Async ODM (Object Document Mapper) for MongoDB based on python type hints
BeanieODM/beanie: Asynchronous Python ODM for MongoDB

Scaling

Replication — MongoDB Manual
Sharding — MongoDB Manual

MongoDB is web scale
Indexing, Replicating, and Sharding in MongoDB [Tutorial] | Packt Hub
Configuring a MongoDB Replica Set for Analytics - DZone Database

Data Modeling

Data Modeling Introduction — MongoDB Manual
Database References — MongoDB Manual
Model Data for Atomic Operations — MongoDB Manual

Modelling Entity Relations In MongoDB | Alexander Paterson populating reference with $match and $unwind
如何将关系型数据导入 MongoDB?

Mongoose v5.12.13: Query Population
mongoose: Referencing schema in properties or arrays | Alexander Zeitler

A Node.js Perspective on MongoDB 3.4: Collations | www.thecodebarbarian.com

Thinking in Documents: Part 1 | MongoDB
Thinking in Documents: Part 2 | MongoDB
MongoDB - Thinking in Documents - agile-code.com

6 Rules of Thumb for MongoDB Schema Design: Part 1 | MongoDB
6 Rules of Thumb for MongoDB Schema Design: Part 2 | MongoDB
6 Rules of Thumb for MongoDB Schema Design: Part 3 | MongoDB

Aggregation (Join)

MongoDB 5.0 deprecated map-reduce and improved aggregation pipeline

Aggregation Operations — MongoDB Manual
Aggregation Pipeline Operators — MongoDB Manual
Aggregation Reference — MongoDB Manual
MongoDB Aggregation: tutorial with examples and exercises | Studio 3T

$lookup is used for join, $unwind is used for flatten array (expanding to multiple documents so the array elements are top level documents)
MongoDB Join Two Collections Simplified | Hevo
MongoDB $lookup Example | MongoDB Aggregation Pipeline

Tips and Tricks

Backup MongoDB inside of Docker the right way - cupcakearmy

Using MongoDB as realtime DB with nodeJS. - Noteworthy - The Journal Blog need replica sets instead of stand along server

How to traverse MongoDB super-large collections efficiently? | Develop Paper
Henrique S. Coelho - hcoelho.com - Fixing memory problems with Node.js and Mongo DB

node.js - Mongoose (mongodb) batch insert? - Stack Overflow no need to use Bulk API, Model.collection.insertMany() is fast enough (and without out of heap issue) (Model.insertMany() suffers from these issues)
javascript - Bulk insert in MongoDB using mongoose - Stack Overflow
Bulk Operations in MongoDB - dbKoda - Medium
Guy Harrison - Yet Another Database Blog - Bulk inserts in MongoDB

mongodb - Difference between findOneAndDelete() and findOneAndRemove() - Stack Overflow prefer findOneAndDelete()

mongodb - Possibility of duplicate Mongo ObjectId's being generated in two different collections? - Stack Overflow

feliixx/mgodatagen: Generate random data for MongoDB

Call function

Using $function in updateMany not working - Working with Data - MongoDB Developer Community Forums

the function has to be stringified

collection.updateMany(QUERY, [
  {
    $addFields: {
      field: {
        $function: { args: ["$field"], lang: "js", body: "function() {}" },
      },
    },
  },
]);

MangoDB

A truly Open Source MongoDB alternative - getmango expose PostgreSQL as MongoDB API