Quiz Entry - updated: 2026.06.20
How do you connect to MongoDB from Node.js?
Import MongoClient from the mongodb driver and instantiate it with a mongodb://host:port connection string.
import { MongoClient } from 'mongodb';
const client = new MongoClient('mongodb://localhost:27017');
Connection string format: mongodb://<host>:<port>
<host>- server hostname or IP (e.g.,localhost)<port>- MongoDB port (default: 27017)
Note: Connection is established lazily on first database access, not when creating the client.