Using a Driver
Real applications don't talk to MongoDB through mongosh — they use a driver library. Here's the same insert-then-query pattern from this course, written with Node.js's official MongoDB driver.
Every language MongoDB supports has an official driver — a library that translates method calls into the same wire protocol mongosh uses under the hood. If you've taken this site's Node.js course, this will look familiar.
Connecting and inserting
JS app.js
const { MongoClient } = require("mongodb");
async function main() {
const client = new MongoClient("mongodb://127.0.0.1:27017");
await client.connect();
const db = client.db("blogdb");
const posts = db.collection("posts");
const result = await posts.insertOne({
title: "Using the Node.js driver",
author: "Priya",
views: 0
});
console.log("Inserted with id:", result.insertedId);
await client.close();
}
main();
Terminal output
Inserted with id: 65f1a5104c1a2b3c4d5e6f80
Querying from code
JS app.js
async function main() {
const client = new MongoClient("mongodb://127.0.0.1:27017");
await client.connect();
const posts = client.db("blogdb").collection("posts");
const results = await posts.find({ author: "Priya" }).toArray();
console.log(results);
await client.close();
}
main();
Terminal output
[
{
_id: new ObjectId("65f1a5104c1a2b3c4d5e6f80"),
title: 'Using the Node.js driver',
author: 'Priya',
views: 0
}
]Notice the method names — insertOne, find — are identical to what you've been typing in mongosh all course. mongosh is really just a JavaScript environment with the driver already connected and loaded for you; learning it directly transfers to real application code.
Note: always
await client.close() (or reuse a single long-lived client for your whole application) rather than opening a new connection for every request — each connection has real setup overhead, and most drivers include a built-in connection pool specifically so you don't need to manage that yourself.Course complete: that covers the MongoDB course from top to bottom — the document model, databases and collections, inserting, querying, updating, and deleting documents, indexes for performance, the aggregation pipeline for reshaping data, the embedding-vs-referencing design decision, and finally talking to MongoDB from real application code through a driver. From here, the natural next step is pairing this with a language-specific course like Node.js or Python to build something real on top of it.