Skip to main content

Getting started with Mongoose, a beginner's perspective

Mongoose is an ODM that takes away the complexity of all the code you'd have to think and write and that you realise that could be abstracted and reused between all pieces of Entities your application will require.

Two main features will help you from the beginning

  • Handles the connection to the MongoDB instance using the official driver behind the scenes
  • Allows to describe your Entities, or Documents how I should name them better, using Schema implementation that offers you the Model, as expected in the MVC pattern.

The Schema is what you need to look into first, than Models.

The Models will allow you to do all CRUD operations, supports pre built in Validation that can be extended with any custom rules, comes with built in Getters and Setters and allows any custom ones.

Clever feature to keep in mind

Also, Mongoose allows you to hook plugins. This is what DRY really means. Any small problem that can be solved and it's solution reused in Models is a prime candidate to be a plugin. You might be even lucky to find already built Mongoose plugins in the community that are well maintained.

One notable plugin I've found useful is the mongoose-version that helps me implement versioning and moderation states. The quick usage would be like revisions of posts in WordPress.

Running MongoDB on your local working environment

My choice for services has been Docker for many years. Running MongoDB is just one easy thing to run it with Docker. I'm using Kitematic to control my Docker containers and for any project or any test, I'm just starting a new container, a MongoDB container in this case, rename it and on the the container settings, I'm hitting save on the Hostname / Ports page to keep the localhost port persistent between restarts.

Conclusion

Mongoose has more features than what I've mentioned as a starting point for a beginner, but those are even for me something to look more in the future. Just the above are decent enough to get me started in describing my main Entities for my second hand cars selling application.

Have fun using MongoDB with this library and feel free to share any other thoughts in comments.

Comments

Popular posts from this blog

New Year's Resolutions 2018 edition

So there it ends another year and I'm deciding on my New Year's Resolutions 2018 list. More than 7 years since I've switched to web development and never sorted out to do something of my own, and not even accomplished some of my previous thought goals. I haven't given them to much importance, as my focus was always to become better on what can I can learn and improve in the work I do for my employer. All I did was playing with PHP , work on WordPress and Drupal websites, follow what ever the company's rules of doing things have been forced and so on. Although my passion is in web development, I realised that this previous years have not been actually been very productive for me. But now things are changing, as I've found the job that would allow me to focus learning and improving on JavaScript and related stuff. (I have to use pure JavaScript, no libraries or frameworks for implementations). And my New Year's Resolution 2018 list is: Build an MV...