MongoDB is undoubtedly one of the most popular NoSQL database choices today. And it has a great community and ecosystem.

In this article, we’ll review some of the best practices to follow when you’re setting up MongoDB and Mongoose with Node.js.

Pre-requisites for this article

This article is one of the part codedamn’s backend learning path, where we start from backend basics and cover them in detail. Therefore I assume you have some experience with JavaScript (and Node.js) already.

Currently we are here:

If you have very little experience with Node.js/JavaScript or the back end in general, this is probably a good place to start. You can also find a free course on Mongoose + MongoDB + Node.js here. Let’s dive in.

Why do you need Mongoose?

To understand why we need Mongoose, let’s understand how MongoDB (and a database) works on the architecture level.

  • You have a database server (MongoDB community server, for example)
  • You have a Node.js script running (as a process)

MongoDB server listens on a TCP socket (usually), and your Node.js process can connect to it using a TCP connection.

But on the top of TCP, MongoDB also has its own protocol for understanding what exactly the client (our Node.js process) wants the database to do.

For this communication, instead of learning the messages we have to send on the TCP layer, we abstract that away with the help of a “driver” software, called MongoDB driver in this case. MongoDB driver is available as an npm package here.

Now remember, the MongoDB driver is responsible for connecting and abstracting the low level communication request/responses from you – but this only gets you so far as a developer.

Because MongoDB is a schemaless database, it gives you way more power than you need as a beginner. More power means more surface area to get things wrong. You need to reduce your surface area of bugs and screw-ups you can make in your code. You need something more.

Meet Mongoose. Mongoose is an abstraction over the native MongoDB driver (the npm package I mentioned above).

The general rule of thumb with abstractions (the way I understand) is that with every abstraction you lose some low-level operation power. But that doesn’t necessarily mean it is bad. Sometimes it boosts productivity 1000x+ because you never really need to have full access to the underlying API anyway.

A good way to think about it is you technically create a realtime chat app both in C and in Python.

The Python example would be much easier and faster for you as a developer to implement with higher productivity.

C might be more efficient, but it’ll come at a huge cost in productivity/speed of development/bugs/crashes. Plus, for the most part you don’t need to have the power C gives you to implement websockets.

Similarly, with Mongoose, you can limit your surface area of lower level API access, but unlock a lot of potential gains and good DX.

How to connect Mongoose + MongoDB

Firstly, let’s quickly see how you should connect to your MongoDB database in 2020 with Mongoose:

mongoose.connect(DB_CONNECTION_STRING, {
	useNewUrlParser: true,
	useUnifiedTopology: true,
	useCreateIndex: true,
	useFindAndModify: false
})

This connection format makes sure that you’re using the new URL Parser from Mongoose, and that you are not using any deprecated practices. You can read in depth about all these deprecation messages here if you like.

#mongodb #node #javascript #programming #web-development

How to Use MongoDB + Mongoose with Node.js
2.45 GEEK