zedocs
GitHub ↗
Build with Ze

MongoDB

Configure MongoDB and Atlas connections and use native CRUD functions in Ze.

Ze includes MongoDB connections and CRUD functions. The interpreter uses the official MongoDB Go driver internally; applications need only .ze source files.

Start a MongoDB backend

Place ze.json beside server.ze:

{
  "database": {
    "driver": "mongodb",
    "name": "ze_backend",
    "envFile": "atlas-credentials.env",
    "schema": "schema.json"
  }
}

The optional envFile contains MONGODB_URI=mongodb+srv://.... Keep it outside version control. It supports plain or quoted key/value lines and comments beginning with #; it does not execute shell expressions. Process environment MONGODB_URI overrides the file; MONGODB_DATABASE overrides the configured database name. Without envFile, provide the URI through the environment.

Run ze serve or ze start. A connection is checked before serving requests. Relative configuration paths resolve beside the entry file. The runtime owns a pooled database connection, shares it across requests, and cancels operations when their request is canceled. Connection and CRUD operations have ten-second deadlines. Errors omit connection credentials.

The optional schema file defines a collection and MongoDB validator; see schema.json. Startup creates the collection with validation if it is missing. Existing collections remain unchanged, so schema edits require a separate migration. schema.sql and --db apply to SQLite. Existing SQLite records are not migrated to MongoDB.

TLS connections use TLS 1.2 or newer with X25519/P-256/P-384 key exchange for compatibility with Atlas and network devices. Certificate verification remains enabled by default. Individual connection attempts time out after four seconds so retries fit within the ten-second server selection deadline.

Native functions

EnglishBisayaArguments / result
mongo_openabli_mongoURI, database name → connection
mongo_closesirad_mongoconnection → close
mongo_idila_mongo24-character hex string → ObjectId for filters
mongo_findpangitaa_mongoconnection, collection, filter → list of records
mongo_insertidugang_mongoconnection, collection, record → inserted record
mongo_updateusba_mongoconnection, collection, filter, changes → updated record in a list, or empty list
mongo_deletepapasa_mongoconnection, collection, filter → number deleted (zero or one)

pares("key", value, ...) creates a record; pares() creates an empty filter. karon_utc() returns the current UTC timestamp. In a served application, tipiganan() supplies the configured connection.

ibutang laray = idugang_mongo(tipiganan(), "buluhaton", pares("ulo", "Pagtuon", "detalye", "Basa sa Ze", "nahimo_sa", karon_utc()))
ibutang salaan = pares("_id", ila_mongo(laray["_id"]))
ibutang mga_laray = pangitaa_mongo(tipiganan(), "buluhaton", salaan)
usba_mongo(tipiganan(), "buluhaton", salaan, pares("ulo", "Pagpraktis"))
papasa_mongo(tipiganan(), "buluhaton", salaan)

Reads return at most 1,000 documents, newest ObjectId first. Update/delete affect one matching document. Updates use $set and reject changes to _id or operator keys. BSON ObjectIds become hex strings in returned records; BSON dates become UTC strings. Integers become Ze numbers (float64), with its usual precision limits. Transactions, aggregation, and pagination are not exposed yet.

Example and verification

From the Ze repository root, set MONGODB_URI privately, then run:

.\bin\ze.exe serve --entry examples\mongodb\server.ze

The API uses /buluhaton and hex string IDs, such as /buluhaton?id=507f1f77bcf86cd799439011. Use the returned POST id for subsequent requests. The example has no authentication and defaults to localhost.

go test ./... runs offline tests. To run the live CRUD/schema/persistence test, set ZE_MONGO_TEST_URI privately and run go test ./web -run TestMongoLiveCRUD -v. This test creates its own uniquely named database and drops only that database afterward. The account must be allowed to create/drop that test database; Atlas must allow the machine’s network address.

Help make these docs better.Edit this page on GitHub ↗

Find a guide or search the language reference.