Use Node.js? Want to learn MongoDB? This is the blog series for you!

In this Quick Start series, I’ll walk you through the basics of how to get started using MongoDB with Node.js. In today’s post, we’ll work through connecting to a MongoDB database from a Node.js script, retrieving a list of databases, and printing the results to your console.

Prefer to learn by video? I’ve got ya covered. Check out the video below that covers how to get connected as well as how to perform the CRUD operations.

Set up

Before we begin, we need to ensure you’ve completed a few prerequisite steps.

Install Node.js

First, make sure you have a supported version of Node.js installed. The current version of MongoDB Node.js Driver requires Node 4.x or greater. For these examples, I’ve used Node.js 14.15.4. See the  MongoDB Compatability docs for more information on which version of Node.js is required for each version of the Node.js driver.

Install the MongoDB Node.js Driver

The MongoDB Node.js Driver allows you to easily interact with MongoDB databases from within Node.js applications. You’ll need the driver in order to connect to your database and execute the queries described in this Quick Start series.

If you don’t have the MongoDB Node.js Driver installed, you can install it with the following command.

npm install mongodb

At the time of writing, this installed version 3.6.4 of the driver. Running npm list mongodb will display the currently installed driver version number. For more details on the driver and installation, see the  official documentation.

#node.js #node

 Connect to a MongoDB Database Using Node.js
1.15 GEEK