# MongoDB recipe

URL: https://briq.cloud/cookbook/mongo

> A MongoDB server with authentication and a persistent volume.

Tags: database, document

Guide:

- [Connect to a database from outside](https://briq.cloud/docs/guides/connect-to-a-database-from-outside.md): A briq's database reachable from psql, a BI tool or an app elsewhere, over TLS, with a normal connection string.

## Run it

MCP:

```ts
briq_run({
  recipe: "mongo",
  params: {
    "version": "8",
    "size": "small",
    "diskGb": 2
  },
  wait: true
})
```

REST:

```bash
curl -X POST https://api.briq.cloud/v1/briqs \
  -H "Authorization: Bearer $BRIQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "recipe": "mongo",
    "params": {
      "version": "8",
      "size": "small",
      "diskGb": 2
    },
    "wait": true
  }'
# → { "briqs": [{ "id": "bq_…", … }], "outputs": { "MONGODB_URI": "…" }, … }

# ... when done, for each briq id
curl -X DELETE https://api.briq.cloud/v1/briqs/<id> \
  -H "Authorization: Bearer $BRIQ_API_KEY"
```

## Parameters

| Name | Type | Options | Default | What it does |
| --- | --- | --- | --- | --- |
| `version` | enum | 7, 8 | "8" | — |
| `size` | enum | nano, small, medium, large | "small" | — |
| `diskGb` | number | — | 2 | Volume size, in GB |

## Outputs

Returned by `briq_run` once the stack is healthy. Hosts resolve inside your team network; secrets are generated per run.

| Output | Value |
| --- | --- |
| `MONGODB_URI` | `mongodb://root:<MONGO_ROOT_PASSWORD>@mongo.<stack>.internal:27017/app?authSource=admin` |
| `MONGO_HOST` | `mongo.<stack>.internal` |
| `MONGO_USER` | `root` |
| `MONGO_PASSWORD` | `<MONGO_ROOT_PASSWORD>` |

## What it runs

| Briq | Image | Ports | Size | Volume |
| --- | --- | --- | --- | --- |
| mongo | `mongo:8` | 27017 | small | 2 GB at /data/db |

## When to use it

* Your code uses Mongoose, Prisma with MongoDB or the native driver, and needs the real server:
  indexes, aggregation, validation.
* Tests expect `MONGODB_URI`.

## Tips

* **Auth is on**: the URI carries the generated root password and `authSource=admin`.
* **No transactions**: it is a single node, not a replica set, so `session.startTransaction()`
  fails.
* **One database per suite** when several run in parallel against the same briq.
* **Seed with mongosh**: `briq_files_put` a script to `/seed.js`, then `briq_exec` with
  `sh -c 'mongosh -u root -p "$MONGO_INITDB_ROOT_PASSWORD" app /seed.js'`.
* **Connect from outside**: `briq_expose` port 27017 with `protocol: "mongodb"`, for mongosh or
  Compass. See [Connect to a database from outside](https://briq.cloud/docs/guides/connect-to-a-database-from-outside).
* **Bigger dataset?** The volume starts small: pass `diskGb`, e.g. `params: { diskGb: 20 }`. Set it
  when the briq is created, and it counts toward your [plan's disk](https://briq.cloud/docs/cost#plans).
