Nest.js with MongoDB: TypeORM vs Mongoose

January 7, 2022 · Updated April 27, 2022 · 1 min read

Lately, I had the chance to introduce Nest.js to an existing Nx monorepo.

To fit in with the rest of the microservices, I used MongoDB as a database.

When using MongoDB with Nest.js, we can decide between TypeORM and Mongoose, both having a great Nest.js integration by their core team.

I chose Mongoose. Here’s why:

  1. At first glance, TypeORM seemed really promising. It is a TypeScript-native project with extensive usage of decorators. This makes it a great fit for Nest.js. BUT:
  2. TypeORM’s MongoDB support is still in preview and doesn’t seem to leave that any time soon. It doesn’t even support relations!
  3. Mongoose, on the other hand, is old and battle-proven. It has typescript definitions (although it’s no TS-native project).
  4. Also, the Nest.js team has written a neat Nest.js integration for Mongoose in TypeScript, even making use of decorators to build the schema!

Now I still had the question open about which architecture I should use for the Nest.js-Mongoose integration. In the next blog post I’ll explain why I ended up using the repository pattern, even though Mongoose is a full-fledged ORM (ODM).