MongoDB is a modern, general-purpose document-oriented data platform that has been widely paired with Node.js in popular tech stacks such as the MEAN stack (MongoDB, Express.js, AngularJS, and Node.js) and the MERN stack (MongoDB, Express.js, React.js, and Node.js).
npm, Node.js’s package manager, is the bridge that allows the MongoDB Node.js driver to be installed, making it easier for developers to work with MongoDB from inside a Node.js application.
Prerequisites
This blog post walks you through the steps needed to connect to a MongoDB database with the Node.js driver which is distributed as an npm package. To follow along, you will need the following tools:
- Node.js and npm
Node.js is a JavaScript runtime for building fast and scalable network applications, and it comes with npm installed. You can download an installer for your platform from the Node.js website.
- MongoDB Node.js driver
To connect your application with a database, you need a software component, also known as a driver. MongoDB Node.js driver supports database connection, authentication, CRUD operations, and observations, among other features.
- MongoDB Atlas
You can create a database either locally or in the cloud by using MongoDB Atlas, a fully managed cloud database service.
First, make sure you have the compatible version of Node.js. You can do that by running the following command in your terminal:
If you see an error, head over to the Node.js documentation and install the correct Node version.
The Node.js installer also installs npm, the Node package manager. To make sure you have npm installed, execute the following command in your terminal:
If you’re facing an issue with this or any other npm commands, refer to the npm documentation to help resolve them.
Project repository
You can explore the finished code for this project in its GitHub repository. Alternatively, you can follow along and build the project from scratch.