Three Options To Use Mongo With Node (And NestJS)
We will look into each and every one of them, and I will provide an example of how you can use MongoDB in your NestJS application in a headache-free way.
The best tutorial I have found for NestJS + Mongoose is here. The issue I had with it is that I had to write the schema definitions and the typescript interfaces. If you are fine with writing everything two times, one for the Schema and one for the Document as Typescript Interface, maybe this is the best way to go!
NestJS + TypeORM is where you can actually use TypeORM with MongoDB; however, I do not recommend this. If you want to learn more, I would ask you to read this blog post.
NestJS + Typegoose — basically, it uses your domain objects to get the schema from them. And this is what this post is all about. There is a lot of documentation on how you can achieve that; however, I didn’t like most of it; it just looked like too much code. On top of that, ALL tutorials ALWAYS include using a DTO class, and I don’t see a reason to use DTO classes at all in 99% of the cases. DTOs are great, don’t get me wrong, there are many pros of using DTOs, but none of the tutorials on the internet actually explains why they want DTOs, and in fact, none of them need DTOs so I would like to write the easiest straightforward way with NestJS + MongoDB + and TypeGoose.
So, first of all, we will install NestJS CLI, NestJS is very, very similar to Angular, and even if you don’t like Angular, trust me, you will like NestJS since I am actually the same! For a great beginner’s tutorial for NestJS, you can read here find.