Aug 3, 2017

Use mongoose or native Nodejs MongoDB driver - that's the question

To mongoose or not mongoose - that's the question  coming all the time and time again among Node/MEAN stack coders.
Usually I don't take sides on this kind of arguments because discussion can become real hot and quite inflammatory leading to ad-hominem attacks, but sometimes I am asked and have to provide my professional opinion. So when I was asked again recently this Shakespearean question I answered it with a short Shakespearean answer:  "thou shalt avoid mongoose".
Surprisingly the discussion didn't go any further, may be I was convincing or probably my opinion was rejected silently.
Any way I feel I must give a more detailed answer (though not complete), I owe this to the mongoDB  ecosystem and to mongoose maintainers who have done a fantastic job writing/maintaining mongoose code base.
The complete answer is it all depends and there are a lot of factors like:

  • learning curve:
    mongoose can save you from the steep learning curve of the native driver, also will protect you from making critical design mistakes like opening/closing a mongoDB connection on each and every request served instead of reusing a connection from a connection pool - a common bad practice. 
  • use case:
    things are different if you are developing a small POV app, then taking shortcuts offered by mongoose can be beneficial but if you are working on a large scale production system mongoose can be an obstacle you have to fight all the time in order to improve efficiency.
  • point in time:
    there were times when mongoose data validation was really helpful and it was a real plus,  not not so match anymore as currently mongoDB supports validation natively 
  • support/features:
    As mongoose is not directly supported by mongoDB you can expect it to be a step behind relative new features, bug fixing etc.
  • ODM:If you feel you absolutely need the ODM layer provided by mongoose then go for it.
TL;DR: mongoose is good in making sensible design decisions for you that are good enough most of the time and certainly much better than what a newbie coder could possibly make, but if you want to get the extra mile in efficiency/features/support etc... you better use native driver.
PS: Let me quote: