1---
2title: Mongo
3description: Official MongoDB in a Project. Bind Railway.ConnectMongo and ping with the MongoDB driver.
4---
5
6A [`Railway.Mongo`](/providers/railway/mongo) is MongoDB as a Service:
7the official `mongo` image, a Volume at `/data/db`, `MONGO_*` /
8`MONGO_URL` variables, IPv6 bind for the private network, and an
9optional TCP proxy for the public URL. The IaC helper is
10`Railway.mongo`.
11
12Private hostname is `{name}.railway.internal`. From a
13[Service](/railway/compute/services), yield
14[`ConnectMongo`](/providers/railway/connectmongo). From a laptop, use
15`publicConnectionUri`.
16
17## Create Mongo
18
19Pass a Project. Alchemy generates a unique name, password, volume,
20and a public TCP proxy.
21
22```typescript
23const site = yield* Railway.Project("Site");
24const db = yield* Railway.mongo("Db", { project: site });
25```
26
27:::caution[Changing `project` replaces Mongo]
28A new service + volume are created in the new Project. The old
29service and volume are deleted. Data is not copied.
30:::
31
32## Connect from a Service
33
34Yield `ConnectMongo` in the Service's constructor. Provide `ConnectMongoHttp`.
35Pass `conn.connectionString` to the MongoDB driver (or
36`Railway.pingMongo`).
37
38```typescript
39import * as HttpServerResponse from "effect/http/HttpServerResponse";
40import * as Redacted from "effect/Redacted";
41
42export default class Api extends Railway.Service<Api>()(
43 "Api",
44 {
45 project: Site,
46 main: import.meta.url,
47 build: { install: ["mongodb"] },
48 },
49 Effect.gen(function* () {
50 const conn = yield* Railway.ConnectMongo(Db);
51 return {
52 fetch: Effect.gen(function* () {
53 const url = yield* conn.connectionString;
54 const ping = yield* Railway.pingMongo(Redacted.value(url));
55 return HttpServerResponse.json(ping);
56 }),
57 };
58 }).pipe(Effect.provide(Railway.ConnectMongoHttp)),
59) {}
60```
61
62To store Railway's `${{Db.MONGO_URL}}` template instead of a
63resolved URI, pass [`Railway.ref`](/providers/railway/ref)
64(`Railway.ref(Db, "MONGO_URL")`) as a
65[Variable](/railway/data/variables) `value`.
66
67## Public TCP
68
69`public` (default `true`) creates a TCP proxy on 27017.
70`publicConnectionUri` is `{domain}:{proxyPort}` for laptop access.
71
72```typescript
73const db = yield* Railway.mongo("Db", {
74 project: site,
75 public: false,
76});
77```
78
79## Where next
80
81[Postgres](/railway/data/postgres) and [MySQL](/railway/data/mysql)
82are the same shape for SQL.
83[TcpProxy](/railway/networking#tcp-proxies) is the public TCP
84resource.
85The [`Mongo` reference](/providers/railway/mongo) lists every prop.
86The [`ConnectMongo` reference](/providers/railway/connectmongo) is
87the runtime binding.