Chapter 0: Introduction and Setup
- MongoDB URI
- Uniform Resource Identifier
- Used to connect applications and MongoDB instances
- SRV
- <host> points to service record
- No need for manually modifying the connection string when servers in cluster change or rotate out
//URI
mongodb://username:password@<host>:<port>
// SRV
mongodb+srv://<username>:<password>@<host>/<database>[?options]
Chapter 1: Driver Setup
- MongoClient
- To initial connection with the database
- Gather information about a database or collection
- Create direct connections to collections, which we can issue queries against
- The Driver team inspects whether a callback is passed. If none is, the driver will return a Promise automatically
- Basic Reads
- findOne()
- find()
// Method 1:
cursor = await movies
.find({ countries: { $in: countries } })
.project({ title: 1 })
// Method 2:
cursor = await movies
.find({ countries: { $in: countries } }, { title: 1 })
// Aggregation Pipeline
Chapter 2: User-Facing Backend
- Cursor Methods and Aggregation Equivalents
- .limit() -> $limit
- .sort() -> $sort, parameter for cursor method should be an array while for aggregation is key-value pair
- .skip() -> $skip
- Basic Aggregation
- Aggregation is a pipeline
- Pipelines are composed of stages, broad units of work
- Within stages, expressions are used to specify individual units of work
- Expressions are functions
- Aggregation is a pipeline
- Basic Writes
- .insertOne()
- .insertMany()
- upsert -> update if exists, otherwise insert
- Write Concerns
- writeConcern: { w: number/majority }
- 0 : “fire and forget”
- 1: default, write successfully to primary
- majority: slower but more durable
- writeConcern: { w: number/majority }
- Basic Updates
- .updateOne()
- .updateMany()
- upsert { upsert: true}
- Basic Joins
- Join two collections of data
- Expressive lookup allows to apply aggregation pipelines to data before the data is joined
- let allows to declare variables in pipeline, referring to document field in source collection
- “Export-to-language” feature
- Basic Deletes
- .deleteOne()
- .deleteMany()
- When perform a delete
- Collection data will be changed
- Indexes will be updated
- Entries in the oplog will be added
Chapter 3: Admin Backend
- Read Concern
- Different levels of “read isolation”
- Can be used to specify a consistent view of the database
- default -> “local” -> return latest data from the node your application is connected to
- Bulk Writes
- Ordered bulk writes
- Executes writes sequentially
- Will end execution after first write failure
- Unordered bulk writes
- flag {ordered: false}
- Executes writes in parallel
- Ordered bulk writes
Chapter 4: Resiliency
- Connection Pooling
- Reuse database connections
- Subsequent requests appear faster to the client
- Default size of 100
- Connection pools are specific to a database client
- the number of connections in the pool is declared when the client is initialized
- All the connections in the pool are dropped when the client object is terminated
- Robust Client Configuration
- Always use connection pooling
- Always specify a wtimeout with majority writes
- Always configure for and handle serverSelectionTimeout errors
- Error Handling
- Writes with error handle
- Timeout error
- writeConcern error
- Principle of Least Privilege
- Consider what kinds of users and what permission they will have
- Application users that log into the application itself
- Database users
- Administrative database users that can create indexes, import data and so on
- Application database users that only have privileges they require
- Consider what kinds of users and what permission they will have
Final exam: MongoDB will rearrange the order and execute the skip before the limit