Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need, what advantages to use mongoose [closed]

I have just started up with mongodb and I recently gone through Mongoose, an ODM framework.

On the documentation, I couldn't find why we need to use Mongoose. One reason I can give is we can define application schema from Mongoose.

I am looking for more possible reasons, and needs that will attract me to use Mongoose.

Please list all possible advantages and reasons/needs why use Mongoose.

like image 352
codeofnode Avatar asked Aug 30 '13 11:08

codeofnode


People also ask

What are the advantages of using Mongoose?

Advantages of Mongoose module:Collection validation of the MongoDB database can be done easily. Predefined Structure can be implemented on the collection. Constraints can be applied to documents of collections using Mongoose.

Do you need to close Mongoose connection?

You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown. Another possible scenario is to close a connection when a data streaming is done.

What is Mongoose and why use it?

Mongoose is a Node.js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as. SQLAlchemy. for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.

Is it necessary to use Mongoose?

Pros/Cons of using Mongoose: Biggest Pro is that it has the data validation built into it(requirements of what data you will allow to be added or to update your database). It will take some work to build that yourself. (but not THAT hard) It will abstract away most of the mongoDB code from the rest of the application.


2 Answers

Main advantage is abstraction over pure mongo.

Many developers who come from SQL database types feel very uncomfortable working with dynamic collections that have no structure defined. So Schemas in the first place helps with that.
Additionally, it implements validation and other neat features in order to make sure your schema is consistent when inserting/updating/finding documents from collections.

It also creates Model abstraction which makes it easier to work with, so it looks like you are working with just objects rather than pure data.

There are many other goodies like middleware, plugins, population, validation. Please check mongoose docs for more information:

Personally, I prefer pure mongo as it is more consistent with official 10gen mongo docs and does not create abstractions that always costs some limits and rules that you will have to follow.

like image 156
moka Avatar answered Sep 22 '22 17:09

moka


If you are working with Node.js and you are pretty new NoSQL I'd recommend using the the native Node Driver(mongodb) at first.

Reasons:

  1. The syntax between the Node Driver and the Mongo shell is very similar and so you'll get a quicker grasp of how to use MongoDB in general.

  2. Models are only useful when you are scaling into a big application with a large API that needs to be broken up into a MVC system(mongoose being your models).

Pros/Cons of using Mongoose:

Pros:

  • Biggest Pro is that it has the data validation built into it(requirements of what data you will allow to be added or to update your database). It will take some work to build that yourself.(but not THAT hard)
  • It will abstract away most of the mongoDB code from the rest of the application.

Cons

  • Biggest con is starting off with schemas right out of the gate will really defeat the purpose of using NoSQL and it will be hard to experience what is good about having a loose structured data system during the stages of rapid development.

  • Not all of your data operations will nicely fit into a characterization that can be encapsulated with a model. Encapsulation is especially hard initially - unless you have a very clear idea of the data flow before you start (which is ideal, but not easy when you are building something conceptually new and requires a lot of experimentation and change/redesign).

like image 36
Nick Pineda Avatar answered Sep 21 '22 17:09

Nick Pineda