MongoDB
Updated on October 27, 2026
MongoDB
A document-oriented database, suited to schemas that change often or to naturally nested data.
Connection string
MONGODB_URI=mongodb://user:password@host:27017/dbname
Mongoose (Node.js)
import mongoose from "mongoose";
await mongoose.connect(process.env.MONGODB_URI!);
const PostSchema = new mongoose.Schema({
title: { type: String, required: true },
content: String,
createdAt: { type: Date, default: Date.now },
});
export const Post = mongoose.model("Post", PostSchema);
PyMongo (Python)
from pymongo import MongoClient
client = MongoClient(os.environ["MONGODB_URI"])
db = client.get_default_database()
posts = db.posts.find({"published": True})
Indexes
Unlike a relational engine, MongoDB does not create an index on your filter fields by default. Remember to declare them explicitly as soon as your collections exceed a few thousand documents:
await Post.collection.createIndex({ createdAt: -1 });