Skip to content

MongoDB

November 5, 2023
December 17, 2014

MongoDB after 3.6 uses SSPL

ferretdb

MongoDB | Build Faster. Build Smarter. | MongoDB

MongoDB Evolved – Version History | MongoDB
MongoDB Evolved – Version History | MongoDB
MongoDB 4.2 vs 4.0, 3.6, 3.4, and 3.2 Benchmarks - hartator - Medium
3.4 is faster

Online Courses | MongoDB University
About M001

Reference — MongoDB Manual
What is MongoDB? — MongoDB Manual
MongoDB - Back to Basics - YouTube
MongoDB 6.0 Brings Encrypted Queries, Time-Series Data Collection – The New Stack

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
MongoDB: A Beginner’s Guide – Bret Cameron – Medium
Stock Price Notifications with Mongoose and MongoDB Change Streams | www.thecodebarbarian.com
mongodb - Difference between findOneAndDelete() and findOneAndRemove() - Stack Overflow prefer findOneAndDelete()

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 — MongoDB Manual
Authentication — MongoDB Manual
Role-Based Access Control — MongoDB Manual

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

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

MongoDB CRUD Operations — MongoDB Manual
Operators — MongoDB Manual

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'})

## 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 — MongoDB Database Tools
mongorestore — MongoDB Database Tools

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

mongoexport/mongoimport

mongoexport — MongoDB Database Tools
mongoimport — MongoDB Database Tools

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

Analyze Query Performance — MongoDB Manual
Query Plans — MongoDB Manual

Partitions — MongoDB Realm
Partition Strategies — MongoDB Realm

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

MongoDB Performance 101: How To Improve the Speed of MongoDB App
Indexing Strategies — MongoDB Manual
How To Use Indexes in MongoDB | DigitalOcean

Indexes — MongoDB Manual
Compound Indexes — MongoDB Manual
Multikey Indexes — MongoDB Manual for arrays in documents
Hashed Indexes — MongoDB Manual when index field > 1024 bytes
db.collection.createIndex() — MongoDB Manual

[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 (Legacy) — MongoDB Manual
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 — MongoDB Manual
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 — MongoDB Manual
Expire Data from Collections by Setting TTL — MongoDB Manual
How to Erase Expired Docs Automatically with MongoDB (TTL index) | Hacker Noon
MongoDB Auto Expire Documents - DEV Community

Sessions API

Since 3.6, provides causal consistency, multi-document transactions

Transactions | MongoDB
Transactions — MongoDB Manual
Session — MongoDB Manual

Mongoose: Transactions

Schema Validation

Schema Validation — MongoDB Manual
$jsonSchema — MongoDB Manual

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
node.js - MongoDB: output 'id' instead of '_id' - Stack Overflow

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
roman-right/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

Perform Two Phase Commits — MongoDB Manual

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

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 - 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