MongoDB typed documents — Developer Preview

SDK 0.19.0 adds this native typed-document binding. SDK 0.18.0 does not contain MongoDB. Check [the current checkpoint](NEXT-RUN.md) and the matching versioned release report for implementation, exact-artifact and publication status.

The API supports one complete flat document at a time, selected by an application-supplied string _id. It does not expose arbitrary query documents, operators, projection, multi-document results, aggregation, change streams, session handles or transactions. PostgreSQL transactions do not enlist MongoDB operations. Developer Preview does not imply production readiness or untested server/platform compatibility.

Native language API

CallResultMeaning
mongodb_find(collection, id, RecordType)RecordType?!Find the exact string _id; absence is none, a valid document is some(record), and schema/transport failure is an error
mongodb_insert(collection, record)String!Insert a complete record and return its supplied _id; an existing _id is an error
mongodb_replace(collection, record)Int!Replace the complete document with the record's _id; return matched count 0 or 1, with no upsert
mongodb_delete(collection, id)Int!Delete the exact string _id; return acknowledged deleted count 0 or 1

collection and id are Strings. RecordType names a declared flat record with a required non-Optional _id: String field. All other fields are Int, Bool, String or their Optional forms. Calls suspend cooperatively without await. Browser Check accepts supported syntax, while browser Run rejects database execution rather than simulating a result.

type User:
  _id: String
  name: String
  active: Bool
  note: String?

fn user_name(id: String) -> String:
  found = mongodb_find("users", id, User) or return "Database unavailable"
  user = found or return "User not found"
  return user.name

fn main():
  print(user_name("user-42"))

This read-only example needs a configured fixture and a collection containing documents that match the declared schema. The first or handles a failed operation; the second handles a successful lookup with no document. An Optional field inside a present record is independent of that outer absence.

Identifiers and field values are encoded as BSON values, not parsed as query operators. For example, an _id containing {$ne: null} remains a literal String. Collection names select a collection; applications should still choose which names their callers are permitted to access. This binding is not an authorization layer.

BSON codec contract

ThreadOne fieldWrite representationAccepted read representation
Int / Int?BSON Int64BSON Int64 exactly, or Int32 widened without loss
Bool / Bool?BSON BooleanBSON Boolean only; numeric 0/1 is not a Bool
String / String?BSON StringBSON String only
Optional noneBSON NULLBSON NULL or a missing Optional field
Required _id: StringCaller-supplied BSON StringThe exact requested String _id; no ObjectId conversion

Decode matches by field name, independently of BSON key order. Unknown fields are errors; there is no projection hiding them. Missing required fields and required fields containing NULL are errors. Empty text is present data. Missing Optional and explicit NULL intentionally collapse to the same none; this API cannot distinguish them. Inserting or replacing Optional none writes a NULL field rather than omitting it.

Floating point, Decimal128, ObjectId, dates/timestamps, binary values, arrays, nested documents and other BSON types are unsupported. There is no numeric truncation or automatic JSON/text conversion. In particular, MongoDB's automatically generated ObjectId documents do not match this API: create documents with application-supplied String _id values.

mongodb_replace replaces the whole matching document rather than applying $set or a patch. It returns **matched count**, so replacing an existing document with identical content still returns 1. No match returns 0 and creates nothing. Use explicit application logic when a different update contract is needed. One operation's acknowledged result is not an atomic transaction across several calls.

Configuration and verified TLS

Set THREADONE_MONGODB_URL and THREADONE_MONGODB_DATABASE outside program source. The database variable is required and selects the operation namespace independently of URI authentication defaults. Keep authSource and credentials in the connection configuration as needed for the fixture's user.

TLS is required and verifies both the certificate chain and hostname. For an internal CA, tlsCAFile is a URI option containing the path to a readable certificate file. The URI may also specify the driver's client-certificate option when applicable. Plaintext, invalid-certificate acceptance and hostname-verification bypass are rejected. Unacknowledged writes (w=0) are rejected. Error messages omit the supplied URI, credentials and document values.

The first successful initialization retains the shared client and its configuration for the process lifetime. Change configuration by starting a new application process. A failed initialization does not turn into a successful cached connection; a later explicit operation may try initialization again. This does not automatically retry a document write.

Admission, lifecycle and cancellation

ResourceLimit and scope
Active binding calls8 shared process slots
Pending callsAt most 32 admission waiters
Admission wait5 seconds
Initialization plus operationA further 5 seconds after admission, including waiting for shared initialization
URIAt most 16 KiB UTF-8
Database nameNonempty, fewer than 64 UTF-8 bytes; namespace validation rejects forbidden characters
Collection nameNonempty, at most 128 UTF-8 bytes; NUL and $ rejected
Application _idNonempty String, at most 256 UTF-8 bytes; NUL rejected
Record schema1–64 unique ASCII identifier fields, each at most 64 bytes; required _id: String
DocumentAt most 1 MiB encoded BSON, including field names and BSON overhead
Driver poolMaximum 8 connections per server, minimum 0; up to 2 connecting per server, with monitoring connections additional

Input encoding preflights the complete BSON size before copying Strings into the driver document. Read checks happen after the driver has received and decoded BSON, before accepted values are copied into the application arena. The entire accepted row's String payload is checked against the remaining arena capacity before arena copies begin. Driver buffers, topology metadata, TLS state and allocation overhead are additional; the document and arena limits do not bound all wire allocations or process memory.

The shared MongoDB client manages connection pools and topology monitoring through background driver tasks. The single ThreadOne application executor does not mean every dependency avoids operating-system threads. Eight active calls is an application admission bound, not eight total driver tasks or a global eight-connection/server-work cap. Pool limits apply per server, and monitoring/cleanup may outlive the initiating call. No language pool handle or shutdown API is exposed in this milestone.

Ordinary and adaptive driver retries are disabled. Timeout, cancellation or a lost response may occur after a write was applied. Reconcile application state by _id before deciding whether to repeat a write; do not assume an error means rollback. Dropping a future does not prove immediate server cancellation. The pinned driver discards a pooled connection returned with an unfinished command rather than reusing a partially read protocol stream; pool cleanup is asynchronous.

Deadlines are cooperative and cannot preempt a CPU loop that does not yield. An HTTP handler deadline may expire earlier than the database operation budget. Session/transaction, failover, replica-set and sharded-topology semantics require their own implementation and evidence; they are not implied by this standalone CRUD API.

Example and evidence

Use [the CRUD program](../sdk/examples/mongodb.one) with [its setup notes](../sdk/examples/mongodb-README.md) against an explicitly selected disposable fixture. It verifies exact integer storage, Unicode and operator-looking String data, Optional NULL versus present empty text, full replacement, deletion and absence. It refuses to start over a preexisting example document.

The separately committed typed Rust foundation has 11 unit tests, five typed live tests and one raw live test against an owned verified-TLS/SCRAM MongoDB 7.0.48 fixture. See docs/validation/2026-12-04-mongodb-foundation. Those results are not evidence that the language binding or final SDK archive passed. New compiler, generated-language and exact-artifact results belong in the matching release report after execution. MongoDB 8.0.38 could not start in the earlier environment because its TCMalloc initialization required unavailable sysfs CPU files; no 8.x compatibility pass is claimed. Linux source tests do not establish clean-OS installation, Windows/macOS/ARM64 compatibility or production readiness.

สรุปการใช้งานภาษาไทย

MongoDB binding นี้อ่านและเขียน document เดียวโดยใช้ _id: String ที่แอปกำหนดเอง รองรับ mongodb_find, mongodb_insert, mongodb_replace และ mongodb_delete สำหรับ native SDK ส่วน browser ตรวจ syntax ได้แต่ไม่รันฐานข้อมูล เพิ่มใน SDK 0.19.0 โดย SDK 0.18.0 ยังไม่มี binding นี้; ตรวจหลักฐานและสถานะเผยแพร่จากรายงานรุ่น

ตั้ง THREADONE_MONGODB_URL และ THREADONE_MONGODB_DATABASE ภายนอกซอร์ส ชื่อฐานข้อมูลใน URI ไม่แทนค่าตัวแปร database หากใช้ CA ภายในให้ใส่ path ผ่าน tlsCAFile ใน URI ใบรับรองและชื่อโฮสต์ต้องผ่านการตรวจ ห้ามใส่รหัสผ่านในโปรแกรมหรือ Playground client ที่เริ่มสำเร็จครั้งแรกจะเก็บการตั้งค่าตลอดอายุ process จึงต้องเริ่ม process ใหม่เมื่อเปลี่ยนค่า

mongodb_find คืน Record?! ให้จัดการ error ก่อน แล้วจึงจัดการกรณีไม่พบ document ภายใน record รับ Int, Bool, String และ Optional เท่านั้น Int เขียนเป็น BSON Int64 และอ่าน Int32 ได้โดยไม่สูญเสียค่า ชื่อ field ต้องตรง แต่ลำดับไม่สำคัญ field เกินหรือชนิดไม่ตรงเป็น error ข้อความว่างเป็นข้อมูลที่มีอยู่ ส่วน Optional ที่หายไปหรือเป็น BSON NULL จะอ่านเป็น none เหมือนกัน ไม่มีการแปลง ObjectId, ตัวเลขทศนิยม, วันที่, binary, array หรือ document ซ้อนให้อัตโนมัติ

mongodb_replace แทนที่ทั้ง document และคืนจำนวนที่ตรง _id ไม่ใช่จำนวนที่ค่าเปลี่ยน หากไม่พบจะคืน0และไม่สร้างใหม่ mongodb_delete ลบเฉพาะ String _id ที่ตรงกัน การส่งข้อความที่ดูเหมือน operator ยังคงเป็น String ไม่ใช่ query ไม่มี session/transaction/multi-document API และไม่เข้าร่วม PostgreSQL transaction

ขอบเขตหลักคือ8 callsพร้อมกัน/32รอ รอ admissionไม่เกิน5วินาที แล้วเริ่ม clientและทำงานภายในอีก5วินาที documentไม่เกิน1MiBรวมโครงสร้าง BSON การตรวจผลเกิดหลังdriverถอดรหัสแล้วจึงไม่ใช่ขอบเขตหน่วยความจำทั้งหมด driverมีpoolต่อserverและbackground monitoringแยกต่างหาก จึงไม่รับรองว่าconnectionหรือtaskทั้งหมดมีไม่เกิน8

คำสั่งไม่ retry อัตโนมัติ หาก timeout, cancellation หรือขาดการเชื่อมต่อ ผลเขียนอาจสำเร็จไปแล้ว ต้องตรวจด้วย _id ก่อนตัดสินใจลองใหม่ การหยุดรอฝั่งแอปไม่ยืนยันว่าserverหยุดทันที ผล MongoDB7.0.48 เป็นของfixtureที่ระบุเท่านั้น ไม่ใช่การรับรอง MongoDB8 ทุกplatform หรือ production