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
pgconnection 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:
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
mongo-intro/
├── package.json
└── index.js
Installing the Driver
mkdir mongo-intro && cd mongo-intro
npm init -y
npm install mongodb
Connecting with MongoClient
// 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();
node index.js
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:
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();
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
- Install MongoDB on your machine, following the instructions for your operating system, and confirm it’s running.
- Install the
mongodbdriver in a new project, and connect withMongoClient, confirming the exact output shown above. - Access a database and a collection with
client.db(...)anddb.collection(...), using names of your choosing. - Explain, in your own words, why MongoDB doesn’t need an equivalent to PostgreSQL’s
CREATE DATABASEstep.
Recap
- MongoDB installs as a background service, listening on
localhost:27017by default, no separate command-line client install required for basic use. MongoClient, from the officialmongodbnpm package, is the direct equivalent ofpg’sClient,connect()opens the connection,close()ends it.client.db(name)anddb.collection(name)access a database and collection, both created automatically on first write, no upfrontCREATEstep needed.
Next lesson: basic CRUD, insertOne, find, updateOne, and deleteOne.