MongoDB vs Mongoose: Which to use and when to use.
Introduction
In my few years of code writing and applications building, I have come across various cases and uses of both MongoDB driver and Mongoose. In this article I'll focus on using Node.js as the backend.
So what is MongoDB, MongoDB is a document modelled database that have come to be widely accepted in the tech community as a whole. Its use cases are numerous but still distinct from other types of database like relational databases (SQL, Postgresql), graph databases like dgraph, etc. Because of it's nature of being non-relational MongoDB generally do not enforce schemas which made it a little weird at first for programmers coming from sql or similar to sql backgrounds. Thus the need for Object Data Modelling , and that's when Mongoose came in as an ODM library for MongoDB to help enforce Schemas.
Using schemas
Schemas are very helpful for strict data type being enforced, maintenance of structure and easy to relate to sql databases. So let's see some use cases where we see the need for schemas.
Database with multiple sub documents: Querying for sub-documents in MongoDB can sometimes be very frustrating and I am a witness of it. Going to the documentations, you even get to see that when performing equality matches on sub-documents, field order matters and the sub-documents must match exactly.
Databases with complicated structure: When working on complicated databases with multiple levels of structure, schemas makes it easier and predictable. Like arrays of objects, possibly containing arrays with objects, looking at the data directly might get you confused and frustrated, but schemas gives insights while guaranteeing data format
Another good reason is the restriction to change the shape of your document unnecessarily. Imagine writing a object to a former named string value, MongoDB tends to write this slowly due to the change in shape of the document, thanks to resizing.
Mongoose is not the only ODM library, there are hapijs/joi, MongoDB schemas, etc. And while Mongoose is good especially in areas of inferring data types, we should choose to use the MongoDB schema validation for schemas validation.
So why use MongoDB schema validation over Mongoose?
First, we should know that mongoose is not officially from MongoDB team and while it helps in ODM, it could have unintended consequences in the future especially for large projects and can't be helped or mitigated by the MongoDB team. Also, Relationship(s) between the Mongoose schema and our database model exists only within the confines of our Node.js application.
But yes, you can leverage the power of schemas and the uniqueness of the MongoDB model together using MongoDB schema validation. You can check how to go about it here.
Don't use Schemas, why?
While using schemas, you tend to miss the whole point of using MongoDB especially in a dynamically typed language like javascript or python. MongoDB provides you a non-relational document type database that can give rise to data evolution, data form manipulation, support for multiple forms of data while maintaining separation of concerns.
Of course you'll want me to explain further, so let's see some reasons in details.
The real world is messy: If you are on twitter you'll get to see many jokes about how users are supposed to use their products vs how users try to use it. Increase in users gives rise to new cases, and I can assure you that you can't get to predict which it would be. This sometimes leads to downed applications which harms the application reach in so many ways. Many a times I have always faulted the allow only this and block every other, rather it should be allow every other except this.
Freedom from limited data types: With MongoDB you don't get to tie yourself or users down with a single form that data should take, you tend to miss out on the tools and feature that MongoDB gives you to project your data to whatever you desire.
Performance: It isn't news that MongoDB drivers performs better than Mongoose and if you want to read more on this check it out here
The Ecosystem: MongoDB is expanding more and more, now we have MongoDB Switch for serverless, MongoDB Charts and all these are supported by the native drivers unlike third party libraries like Mongoose.
MongoDB is giving you a lot more with tools and features not forgetting Compass UI too we have to utilize these tools for our ease rather than limitations. Yet, if there is any needed use cases to use schemas, I'll advice using MongoDB schemas validation which through the native drivers helps validate your data properly. Mongoose is good and don't get me wrong, I'm not telling you to drop it suddenly I'm only showing you the need for a better tool and while Mongoose may help those coming from sql backgrounds, MongoDB using native drivers is not hard to learn and very useful.