When to use MongoJS vs Mongoose
The two most popular libraries for this are MongoJS and Mongoose which both are available through npm, the node package manager. In this article we’ll talk about the two libraries and try and give you enough background information to make the decision for yourself.
Are they basically the same thing?
You might be wondering that since they seem to accomplish the same functionality if there really is a difference between the two and if the decision between the two really matters. In short, yes they are very different, and yes it matters. For the most part the two libraries can get you the same results but the way you get there will be extremely different and your code will be extremely different. So it is worth the time of studying the difference between these two libraries and making an informed decisions for your application.
The Two Main Differences
In our experience there are two main differences between these two libraries.
MongoJS is simpler and requires less work upfront. This library will give you all the functions that you can execute in the Mongo Shell and with similar syntax. Mongoose on the other hand runs on top of MongoJS and is an abstraction of that library known as an Object Document Mapping or ODM. MongoJS will let you get started writing immediately with functions that feel very familiar, while Mongoose will require you to create Schemas and Models before you get down to creating documents.
Mongoose is based on creating schemas and models for your documents. You might be wondering why we’d anybody would use Schema’s since this is a NoSQL technology whose whole premise is that it doesn’t have to have a specific schema. To be fair you’d have a good point, but for many production and non-production environments alike it is helpful to define a schema for your data. Playing fast and loose with your document structure can come back to bite you in the long run. In Mongoose you’d need to define Schemas for all your data much like you would in SQL, detailing fields, datatypes, defaults, and a myriad of other options. Once you had a Schema you’d then make that into a Model which then would let you create instances of that Model in the database.
Should I use them both?
Some people wonder if they should use them both. While this is possible it’s very confusing and bad practice and we suggest you pick the library you think fits your application best and stick with it.
From our experience Mongoose seems like the better choice in the long run. Once you get over the hump of setting up your Models it becomes very easy to work with and provides loads of functionality like built-in automatic validation when inserting data. It also gives lets you setup events to run at certain points in the execution, for example before a document gets saved. This middleware and validation is extremely helpful for most developers.
How to Install Mongoose and Mongojs
Installing either library is very easy once you have NodeJS installed. You’ll initialize you’re folder with
Then you can install MongoJS like this
Or install Mongoose like this
We have described a few of the difference between the two libaries and we hope that you can make a more informed decision about your own choice. Of course we recommend you checkout each library’s documenation and look specifically at code samples so you can get an idea of the type of code you’ll be writing since it will be extremely different depending on the library you choose. If you have any more implementation details or feedback on this article don’t hesitate to reach out.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started