Mongoose's growth over the last few years has been astonishing. Mongoose is now the most popular database framework for Node.js, both in terms of weekly downloads on npm and in terms of repos using it on GitHub. It's also stood the test of time: Mongoose celebrated its 10th birthday this year.

Mongoose is a powerful framework with an active community. But, based on the 80k StackOverflow questions, there's a lot of confusion out there about how you should build Mongoose apps.

The Mongoose documentation is focused on helping developers solve problems given a wide variety of different architectures, environments, and experience levels. Unfortunately, that approach leaves a gap for a more opinionated guide that explains Mongoose fundamentals from base principles.

Introducing Mastering Mongoose

Mastering Mongoose is a carefully curated guide that explains the fundamentals of Mongoose and how they relate to building apps in different frameworks. It distills 8 years of experience building Mongoose apps with wildly different frameworks, team compositions, and budgets into 153 concise pages. You'll learn:

  • The 5 fundamental classes in Mongoose: Model, Document, Schema, Connection, and Query
  • 3 schema design principles for ensuring your database queries stay fast when your collections grow beyond 100k documents
  • How to use the 4 different types of middleware in Mongoose
  • Design patterns for modeling one-to-many and many-to-many relationships in Mongoose using populate()
  • When you should use middleware as opposed to setters or validators, and vice versa
  • How to determine what indexes you need, and how to evaluate the effectiveness of your indexes
  • 2 different patterns for implementing pagination: one for convenience, and one for performance
  • How to build HTTP apps with Express and Mongoose
  • How to implement JWTs and access tokens with Mongoose
  • 4 different design patterns for instantiating Mongoose models, and the tradeoffs between them
  • How to build Websocket apps with ws and Mongoose

Mastering Mongoose also comes with 4 sample apps that demonstrate how these lessons come together in building a real app.

Since Mastering Mongoose is a guide rather than a complete reference, what the eBook omits is as important as what it includes. Here are some of the more controversial choices:

  • Aggregation framework. The eBook includes a brief aside on aggregation middleware, but explicitly excludes going into detail about how to write aggregations. I believe that most Mongoose apps are better off using cursors and aggregating in memory for performance and readability reasons.
  • Transactions. Many developers don't think you can write a real app without transactions. I strongly disagree. Building an app that transfers currency between accounts? You need transactions. Building a Twitter clone? You don't need transactions.
  • Read preferences and write concerns. Read preferences are great for reading data from the geographically closest server and write concerns are great for ensuring data integrity in the event of an unclean shutdown. But I've worked on MongoDB apps that operate fine at massive scale with no read preferences or write concerns, so I don't think these are "must haves" for every app.

Moving On

Mastering Mongoose contains the hard-earned lessons from 8 years of building Mongoose apps, including what concepts and design patterns are indispensible for building a new app in 2020 and beyond. Check out the online preview at masteringmongoose.com, and get your copy today!.

Found a typo or error? Open up a pull request! This post is available as markdown on Github
comments powered by Disqus