MongoDB Primer
A pragmatic walkthrough of running MongoDB with Meridian: connecting, modeling documents, and querying the way the production graders expect.
MongoDB is the default document store wired into every Meridian sandbox. You get a scoped database, automatic isolation per evaluation run, and a connection string injected as MONGODB_URI. This recipe walks through the three motions you will perform in nearly every brief: connect, write, query. No ORM is required, but the official driver is preinstalled.
1. Connect with the official driver
Use the read-once env var. Keep the client at module scope so connection pools live across requests. The sandbox kills idle pools after sixty seconds, so do not bother with manual cleanup unless the brief asks for it.
import { MongoClient } from 'mongodb';
const uri = process.env.MONGODB_URI ?? '';
const client = new MongoClient(uri);
export async function getDb() {
await client.connect();
return client.db('meridian');
}2. Model documents flat first
Meridian graders test for predictable shapes. Avoid premature normalization: embed child arrays when reads dominate writes, and reach for references only when a child grows unbounded. Always set an explicit createdAtso backfills and audits stay deterministic across replays.
3. Query with projection and limit
The default cursor pulls every field. The grader times your endpoint, so trim to the fields you render and cap the result set. Sort on an indexed field, and only calltoArray after you have narrowed the cursor.