Comparing Prisma and Mongoose

Chike Ibezim
4 min readJan 8, 2023

--

I’ve been using mongoose with MongoDB for most of my projects for over 4-years now because of its structure, and documentation, and also because I lovesome JSON data, lol! Recently, while keeping up with my knowledge of several technologies, I decided to build a mini-project using React as my front-end framework and using GraphQL’s Apollo to design my API for the app.

I used SQLite as my database (great database by the way, especially for embedded systems) and then Prisma to interact with my database. This article is about my personal understanding of the similarities and differences between Mongoose and Prisma and when you could consider using one over the other especially when you want to work with MongoDB

What are Prisma and Mongoose?

Prisma is an Object Relational Model (ORM) while Mongoose is an Object Data Model (ODM) meant for a MongoDB database. Both object-oriented libraries are used to create structured queries to interact with your database. They are a great way to save time building complex queries that interact with a database, especially sequel queries (SQL) for Prisma and as mentioned earlier, MongoDB for mongoose.

Imagine building your backend and writing long SQL Statements such as “Select * from users WHERE role = shopper AND blah blah ….” to fetch data from your database. I consider this way too long and could make your API/backend more complex than it should be. Using ORMs like Prisma helps structure these queries in a simplified manner using an object-oriented approach and helps make a developer’s life easier.

Moving forward, I’ll outline and explain what I noticed when building my latest project using Prisma and compare it with my experience using MongoDB’s mongoose.

1. Prisma can interact with a variety of databases

While mongoose was developed solely for MongoDB, Prisma can interact with a variety of databases which include SQLite, Postgresql, MongoDB, MySQL, and Microsoft SQL. This makes Prisma a great choice for developers using SQL, especially those looking to migrate from their existing format to an OO approach.

2. Prisma is more suited for SQL — Stick with Mongoose

Although you can use Prisma to interact with MongoDB, I feel it’s more suited to SQL databases because of its relational model rather than NoSQL databases such as MongoDB. Asides from Prisma being an ORM, there are also similarities in the syntax such as the “WHERE”, “INCLUDE” etc. When building an application with MongoDB as your database, I personally feel you should stick with mongoose as it was built solely for the Mongo database and offers rich documentation and a vast community of enthusiasts. One reason why you may want to use Prisma over Mongoose might be Schema Enforcing which we’ll see next.

3. Prisma enforces Schema on MongoDB

When working with Mongoose over time, so many of your application structures may change which may require you to change the data types or structure of your database fields. Some fields may change from int to string, or from int to floats. Sometimes, this may affect your data and how it’s used within your application maybe during conversions or how it’s presented. Prisma prevents this from happening by enforcing the data type defined in the schema on the database when migrating. This ensures that all your data are in sync always. No type issues

4. Prisma keeps track of your database history

As your database changes over time, Prisma keeps track of these changes through its migration. These changes are saved in a migrations folder in your Prisma directory. In MongoDB, there’s no automated method of keeping track of these changes

5. Explore the database on Prisma Studio

Prisma provides an intuitive GUI that helps developers interact with their database/models easily. You can view and manage records on the go from your localhost as you work on your application. My experience with mongoose however is different. With Mongoose I always had to modify or interact with my data from the MongoDB website

Summary

These are a few notes I’ve taken so far in the course of building this app. I’ll try to update this as I learn more about Prisma and even Mongoose (especially as versions get updated). In summary, Prisma is best for SQL projects while mongoose is best used with its parent DB (MongoDB). You could try using Prisma in your next project if using a NoSQL database such as MongoDB to try out some of the features mentioned above. In all, it comes down to what fits the needs of your application structure the most.

Will be happy to see contributions, suggestions, and a clap if it helped you in any way :-)

Time for that coffee….

--

--