r/node • u/alexbevi • Mar 22 '24
Best way to work with MongoDB (Mongoose? Driver? Prisma?)
Just a quick question for anyone building MEAN/MERN/MEVN/<other> applications - how do you typically work with MongoDB? Is it better to use the driver directly?
19
Upvotes
17
u/wardrox Mar 22 '24
If you're not doing anything beyond the basics, the official MongoDB driver works just fine.
23
u/bonkykongcountry Mar 22 '24
Mongoose.
5
u/macrozone13 Mar 22 '24
Prisma is great, just not for mongodb, there are severe performance problems.
However the question is, why you think you need mongodb. Many webapplications benefit more from fast relation queries and type safety in my opinion, so favor postgres with prisma. Also you have more hosting options
2
u/tinfoiltank Mar 22 '24
If you're doing basic CRUD operations on data that can be represented with schemas (the majority of applications), you should use Mongoose. If you're doing more complicated stuff where you need to optimize you should use the driver directly.
9
u/mightybjorn Mar 22 '24
I've used Mongoose and the official driver alot, but most projects I work with use Mongoose, it's really solid but super opinionated. My only 2 real complaints are: it's difficult to connect to multiple databases at once (a nice to have for parallelizing integration tests) and the typescript support feels a little wonky.
I've been leaning more towards the official driver with JSON scheme validation at the db level. But Mongoose is a really solid & impressive ORM!
1
u/xersuatance Mar 23 '24
i used mongoose a lot. virtuals and typescript support is good, but a lot of room for improvements, u need to define at least 3 separate things for a single document, 1st type export for the document, 2nd a class for columns and 3rd a schema definition. query performances and populations work great but you dont get type safety in populations. i tried mikroorm on a very simple project. relations and poplulatios were both type safe and easy to work with. however i didnt test mikroorm on a user facing app so cant really tell id its any good on prod. any experiences with mikroorm + mongo on prod?
6
u/saganistic Mar 22 '24
If you're in TS and want to use a library like zod for validation, then Mongoose becomes an extra layer that likely is more trouble than it is worth and the native driver is simpler. If you want to define your schemas in the same "place" that you define queries and methods, and you want to make use of features like virtual fields, then Mongoose does the job.
2
u/edo96 Mar 22 '24
I'm currently using Prisma and it works fine with MongoDB as a long as you perform standard CRUD operations. Sometimes the queries can become more complex and Prisma also offer a raw query mechanism in order to guarantee performance.
It also easy to track indexes and do migrations.
1
u/brightsmyle Mar 27 '24
Typegoose