# How to run MongoDB in a cloud sandbox Add MongoDB's apt repository in an Ubuntu 24.04 microVM, install mongodb-org, start mongod with spawn, and load data with mongoimport. **On Runtime, each test or agent gets its own MongoDB server.** Install MongoDB once in an image, and every sandbox after that has it on disk, ready for `mongod`. Each one is a Firecracker microVM with its own kernel. It costs $0.03125 an hour for 2 vCPU and 4 GiB while it waits on queries ([pricing](/docs/pricing)). ## Where MongoDB comes from MongoDB is not in Ubuntu's archive. There is no `mongodb` package for 24.04. MongoDB's install guide, read 25 October 2026, covers MongoDB 9.0 Community Edition. It lists Ubuntu 24.04 "Noble" as supported on x86_64, which is what a sandbox runs. | Package | What it holds | | -------------------- | -------------------------------------------------- | | `mongodb-org` | The meta-package: server, database tools and shell | | `mongodb-org-server` | `mongod`, about 300 MB installed | | `mongodb-org-tools` | `mongoimport`, `mongoexport`, `mongodump` and more | | `mongodb-mongosh` | `mongosh`, the shell | The installed size is the one MongoDB's noble repository listed for `mongodb-org-server` on 25 October 2026. The official Docker image, `mongo:9.0`, is the other route, with [Docker in a sandbox](/how-to/run-docker-in-a-sandbox). ## Install, start, load, query ```ts check import { writeFile } from "node:fs/promises"; import { Sandbox } from "withruntime"; const repo = [ "sudo curl -fsSL -o /usr/share/keyrings/mongodb-server-9.asc https://pgp.mongodb.com/server-9.asc", 'echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-9.asc ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/9.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-9.0.list', "sudo apt-get update -qq", "sudo apt-get install -y -qq mongodb-org", ].join(" && "); await using sbx = await Sandbox.create({ diskMiB: 6144, timeoutSeconds: 1800 }); await sbx.exec(repo, { check: true, timeoutMs: 600_000 }); await sbx.exec("mkdir -p /workspace/db", { check: true }); await sbx.spawn("mongod --dbpath /workspace/db --bind_ip 127.0.0.1 --port 27017"); await sbx.exec( "timeout 60 bash -c 'until mongosh --quiet --eval \"db.runCommand({ ping: 1 }).ok\"; do sleep 1; done'", { check: true, timeoutMs: 90_000 }, ); await sbx.files.upload("./products.json", "/workspace/products.json"); await sbx.exec("mongoimport --db shop --collection products --jsonArray --file products.json", { check: true, timeoutMs: 300_000, }); const cheap = await sbx.exec( `mongosh shop --quiet --eval 'db.products.countDocuments({ price: { $lt: 20 } })'`, { check: true }, ); console.log("under $20:", cheap.stdout.trim()); await sbx.exec("mongoexport --db shop --collection products --out /workspace/out.json", { check: true, }); await writeFile("out.json", await sbx.files.read("/workspace/out.json")); ``` ```python check from withruntime import Sandbox REPO = " && ".join([ "sudo curl -fsSL -o /usr/share/keyrings/mongodb-server-9.asc https://pgp.mongodb.com/server-9.asc", 'echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-9.asc ] ' 'https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/9.0 multiverse" ' "| sudo tee /etc/apt/sources.list.d/mongodb-org-9.0.list", "sudo apt-get update -qq", "sudo apt-get install -y -qq mongodb-org", ]) with Sandbox.create(disk_mib=6144, timeout_seconds=1800) as sbx: sbx.exec(REPO, check=True, timeout_ms=600_000) sbx.exec("mkdir -p /workspace/db", check=True) sbx.spawn("mongod --dbpath /workspace/db --bind_ip 127.0.0.1 --port 27017") sbx.exec("timeout 60 bash -c 'until mongosh --quiet --eval \"db.runCommand({ ping: 1 }).ok\"; " "do sleep 1; done'", check=True, timeout_ms=90_000) sbx.files.upload("products.json", "/workspace/products.json") sbx.exec("mongoimport --db shop --collection products --jsonArray --file products.json", check=True, timeout_ms=300_000) print(sbx.exec("mongosh shop --quiet --eval 'db.products.countDocuments({ price: { $lt: 20 } })'", check=True).stdout) sbx.exec("mongoexport --db shop --collection products --out /workspace/out.json", check=True) sbx.files.download("/workspace/out.json", "out.json") ``` - The repository line is MongoDB's own for noble, with its key, `server-9.asc`, for the 9.0 repository. MongoDB's guide converts the key with `gpg`, which the default image does not include; apt reads the key as downloaded when its file name ends in `.asc`. - `mongod` runs as the sandbox user with its data in `/workspace/db`. It needs no `sudo`. `spawn` keeps it running after the call returns. - The repository is served over HTTPS on port 443. A sandbox without credit can reach it. ## Hand an agent a database it can wreck A model that writes queries or migrations can get things wrong, sometimes in ways that destroy data. Run its code in the same sandbox, with the internet off once MongoDB is installed. A bad `deleteMany({})` then costs one sandbox: ```ts check import { Sandbox } from "withruntime"; declare const db: Sandbox; // the sandbox above, data loaded declare const agentScript: string; // the query the model wrote await db.network.off(); // the model's code can reach nothing outside await db.files.write("/workspace/agent.js", agentScript); const result = await db.exec("mongosh shop --quiet --file agent.js", { timeoutMs: 120_000 }); console.log(result.exitCode, result.stdout.slice(0, 2000)); ``` Network rules are enforced on the host. Root in the sandbox cannot turn them back on. To give each attempt a fresh copy of the loaded data, fork the sandbox before you run it ([sandbox fork](/glossary/sandbox-fork)). ## Open it in Compass ```bash no-run runtime sandboxes port-forward 27017 ``` Connect Compass, or your app, to `mongodb://127.0.0.1:27017/?directConnection=true`. The forward runs through Runtime's API with your key. It works without credit too. The sandbox opens no port to the internet ([forward ports](/docs/editors#forward-ports)). From code, `sbx.forwardPort(27017)` or `sbx.forward_port(27017)` does the same. ## Start every sandbox with MongoDB installed Recipe commands run as root, so the repository lines drop their `sudo`: ```ts check import { Runtime } from "withruntime"; const runtime = new Runtime(); await runtime.images.build({ name: "mongodb", recipe: { commands: [ "curl -fsSL -o /usr/share/keyrings/mongodb-server-9.asc https://pgp.mongodb.com/server-9.asc", 'echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-9.asc ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/9.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-9.0.list', "apt-get update && apt-get install -y mongodb-org", ], }, }); await using sbx = await runtime.sandboxes.create({ image: "mongodb" }); ``` Building is free. Without credit, a build counts toward the free usage. An account without credit can store three images free ([custom images](/docs/images)). ## Sizing - **Disk.** The server alone is about 300 MB installed, plus the tools, the shell and your data. The default 4 GiB disk had about 2.5 GiB free on 24 September 2026. The samples ask for 6 GiB. Without credit, a sandbox has up to 10. - **Memory.** MongoDB's cache lives in the sandbox's memory. You set that size with `memoryMiB`, and it is billed at $0.0075 a GiB-hour while running. - **Keeping data.** A pause keeps the running `mongod` and its memory. For a disk that outlives sandboxes, put `--dbpath` on a [volume](/docs/javascript#volumes). For relational data, see [PostgreSQL in a sandbox](/integrations/postgres) and [MySQL in a sandbox](/integrations/mysql). ## Sources - MongoDB, install Community Edition on Linux, https://www.mongodb.com/docs/manual/administration/install-community-linux/, read 25 October 2026 - MongoDB noble repository, https://repo.mongodb.org/apt/ubuntu/dists/noble/mongodb-org/9.0/, read 25 October 2026 - Ubuntu 24.04 package indexes, http://archive.ubuntu.com/ubuntu/dists/noble/, read 25 October 2026 - Docker Hub tags for mongo, https://hub.docker.com/_/mongo, read 25 October 2026 Facts on this page were checked on 25 October 2026.