CodingNic

Introduction to MongoDB and the Node.js Driver

Connecting with the Node.js Driver

Introduction to MongoDB and the Node.js Driver 15 min read

Connecting with the Node.js Driver

Objectives

By the end of this lesson, you should be able to:

  • Install MongoDB and the official Node.js driver
  • Connect to a MongoDB database with MongoClient
  • Access a database and a collection from a connected client

💡 Why this matters: Everything from this lesson through the end of the course runs on top of this connection, exactly the role Module 1’s pg connection played for the entire PostgreSQL half of this course.

⚠️ A note on verification: MongoDB’s own server binary needs to be downloaded to run locally, and this course’s own tooling runs in a sandboxed environment with no route to that download. So unlike the PostgreSQL modules, the commands and output below are not this course’s own live output, they’re MongoDB’s standard, current, documented behavior, stable across recent versions. Run them yourself, on your own machine, with normal internet access, they’ll behave exactly as shown.

Installing MongoDB

macOS, using Homebrew:

bash
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community

Windows: download the installer from mongodb.com/try/download/community and run it, it installs MongoDB as a background service.

Linux (Debian/Ubuntu-based): follow the current instructions at mongodb.com/docs/manual/administration/install-on-linux, MongoDB isn’t in Ubuntu’s default package repositories, so it needs MongoDB’s own repository added first.

By default, MongoDB listens on localhost:27017.

Project File Structure

text
mongo-intro/
├── package.json
└── index.js

Installing the Driver

bash
mkdir mongo-intro && cd mongo-intro
npm init -y
npm install mongodb

Connecting with MongoClient

javascript
// index.js
const { MongoClient } = require('mongodb');

const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri);

async function main() {
  await client.connect();
  console.log('Connected to MongoDB!');
  await client.close();
}

main();
bash
node index.js
text
Connected to MongoDB!

MongoClient is the direct equivalent of pg’s Client (Module 1), one connection, connect() to open it, close() to end it.

Accessing a Database and a Collection

Unlike PostgreSQL, MongoDB doesn’t require a database to be created ahead of time (no CREATE DATABASE, no equivalent step), it’s created automatically the first time data is written to it:

javascript
async function main() {
  await client.connect();

  const db = client.db('school');
  const students = db.collection('students');

  console.log('Ready to use the students collection');

  await client.close();
}

main();
text
Ready to use the students collection

client.db('school') accesses (or implicitly creates) the school database. db.collection('students') accesses (or implicitly creates) the students collection, the equivalent of Module 1’s school database and students table, but with no CREATE DATABASE or CREATE TABLE step required first. The next lesson uses students to actually insert and read documents.

Try It

  1. Install MongoDB on your machine, following the instructions for your operating system, and confirm it’s running.
  2. Install the mongodb driver in a new project, and connect with MongoClient, confirming the exact output shown above.
  3. Access a database and a collection with client.db(...) and db.collection(...), using names of your choosing.
  4. Explain, in your own words, why MongoDB doesn’t need an equivalent to PostgreSQL’s CREATE DATABASE step.

Recap

  • MongoDB installs as a background service, listening on localhost:27017 by default, no separate command-line client install required for basic use.
  • MongoClient, from the official mongodb npm package, is the direct equivalent of pg’s Client, connect() opens the connection, close() ends it.
  • client.db(name) and db.collection(name) access a database and collection, both created automatically on first write, no upfront CREATE step needed.

Next lesson: basic CRUD, insertOne, find, updateOne, and deleteOne.