Vast majority of beginner nodejs tutorials use mongodb for the database. For simplicity sake, this is fine for beginners. It saves them the hassle of dealing with SQL table relations, so they can just stick any data in and get it out to render it on the page.

So, you need to persist some data

After learning the basics of building apps with Node.js, you may decide to build an app on your own. The courses and tutorials you learned from taught you Mongodb. It seems like next logical step:

  • It has a nice Javascript API, just like Node.js
  • It stores all data in JSON format
  • You can easily fetch any data from the database
  • You easily send that data to the client
  • It’s blazing fast, just like Node.js
  • You don’t have to learn another language just for databases

It seems like Mongodb is hand crafted for Javascript environment. For the first time ever, we can use one the same language in all 3 layers of our applications:

  • Javascript in the browser (User interface)
  • Javascript on the server (Business logic)
  • Javascript in the database (Data persistance)

Knowing this, its easy for beginners to fall into a trap of thinking Mongodb is the only database to use to persist data in Node.js apps. Experienced developers, coming to Node.js from other technical backgrounds, often ask: “Can I use Postgres/Mysql with Node.js?”

You can use any database you want

Node.js is Javascript runtime environment. It is a symbiosis between a Javascript interpreter and some C++ code that let’s you write server or desktop apps with Javascript language.

This was not possible before, since Javascript is a language meant to be use in the context of a browser, thus confined within it for security reasons. Node.js sets Javascript free.

Having Javascript execute outside of browsers allows us to do things such as networking, file system manipulation, getting operating system information, multi-threading (even though Javascript is a single-threaded language) and much more that was done with most other languages like Java, C#, PHP, Ruby, Python, etc.

Since a Node.js app runs in the memory, a node app would need to preserve data in the database so it doesn’t get lost. Which database can you use with node? Any database that you can find a driver for, which is all of them.

There is nothing in Node.js that dictates your choice of database. It does not favor any particular database system, neither relational nor non-relational.

Database is an external service from the point of view of a node app. You only need a driver and a running instance of a database to use it. Node.js doesn’t care about that at all.

Use whatever database you want.