Drivers Peek (Node and Python)

View saved

Drivers speak the wire protocol

Apps rarely shell out to mongosh. Official drivers provide APIs in Node.js, Python, Java, and more.

Node.js sketch

Install mongodb, connect once, then reuse the client.

// npm install mongodb
import { MongoClient } from "mongodb";

const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();
const books = client.db("shop").collection("books");
const doc = await books.findOne({ title: "Practical MongoDB" });
console.log(doc);

Python sketch

Use pymongo with a similar pattern.

# pip install pymongo
import os
from pymongo import MongoClient

client = MongoClient(os.environ["MONGODB_URI"])
books = client["shop"]["books"]
print(books.find_one({"title": "Practical MongoDB"}))

Connection strings and pooling

Store the URI in an environment variable. Drivers pool connections—create one client per process, not per request.

Comments

One comment per signed-in account. Comments are saved with this page’s URL.