How do you run MongoDB queries inside Cloudflare Durable Objects?
You can keep MongoDB-style collection calls inside a Cloudflare Durable Object by using @semics-tech/mongolite 1.7.0, formerly published as mongolite-ts. Import MongoLite and CloudflareDurableObjectAdapter from @semics-tech/mongolite/cloudflare and pass the adapter to MongoLite, and the Durable Object's SQLite storage handles the rest. You still write find, insertMany and aggregate calls, not SQL.
Durable Objects give you SQLite storage, but not a document model. If you already think in collections and filters, dropping to hand-written SQL and JSON functions for every query is slow and error-prone. This guide walks through the setup and states where the approach stops.
What does the adapter do with Durable Object storage?
The CloudflareDurableObjectAdapter implements the IDatabaseAdapter interface, so MongoLite talks to it the same way it talks to a SQLite file, an in-memory SQLite database or the browser adapter built on sql.js. Each collection maps to a SQLite table. Every table has an indexed _id column and a data column that holds the JSON document. Query operators are translated into SQL where possible, and the remaining logic runs in shared utilities so that find() and aggregation $match behave the same way.
The adapter reads and writes through the storage object you give it, which is ctx.storage.sql inside the Durable Object, with ctx.storage passed as a second argument for transactions. Nothing in the adapter opens a separate database connection, so the data lives where Cloudflare stores the object's state.

How do you set up the Durable Object?
The example below follows the pattern from the README. It has three parts: a typed Env, a call to ensureTable() wrapped in ctx.blockConcurrencyWhile, and a connect() call at start-up. The Env interface is explicit, so there is no any anywhere in the file.
import { MongoLite, CloudflareDurableObjectAdapter } from '@semics-tech/mongolite/cloudflare';
// Explicit binding type for the Worker environment
export interface Env {
ORDERS: DurableObjectNamespace;
}
export class OrderStore {
private readonly client: MongoLite;
constructor(private readonly ctx: DurableObjectState, private readonly env: Env) {
// Pass the SQL API, plus ctx.storage so transactions use the undo log
const adapter = new CloudflareDurableObjectAdapter(ctx.storage.sql, ctx.storage);
this.client = new MongoLite(adapter);
// Hold incoming requests until the table exists and the client is connected
ctx.blockConcurrencyWhile(async () => {
await this.client.connect();
await this.client.collection('orders').ensureTable();
});
}
async fetch(request: Request): Promise<Response> {
const orders = this.client.collection('orders');
const open = await orders.find({ status: 'open', total: { $gte: 100 } }).toArray();
return Response.json(open);
}
}
The constructor runs once per object instance. blockConcurrencyWhile stops other events from reaching the object until the callback finishes, so ensureTable() and connect() complete before the first fetch. Skipping this wrapper can let a request reach a missing table.
The find call at the end uses a filter with $gte, which is pushed down to SQL. The same shape works for findOne, insertMany and replaceOne.

How are transactions handled inside a Durable Object?
Transactions on Durable Objects are emulated with an undo log. They are not native database transactions. Before a write runs, the adapter records how to reverse it. If a multi-step operation fails part way, the adapter applies the recorded reversals. This keeps the logical result consistent for the operations inside the transaction, but the rollback is code you can read in the adapter, not a guarantee from the storage engine.
Two practical consequences follow. First, do not assume the undo log behaves like a SQLite ROLLBACK in every failure case, particularly if the object is evicted mid-operation. Second, keep transactional work small and inside a single Durable Object, since the emulation only covers that object's storage.
Which MongoDB operators and methods are covered?
Parity with the MongoDB client is a goal, not a finished state. The parity suite runs identical scenarios against mongolite and a real MongoDB instance started with mongodb-memory-server, and it is the reference for what is verified. The covered surface in 1.7.0 includes these collection methods:
- find, findOne, insertMany, replaceOne
- findOneAndUpdate, findOneAndDelete, findOneAndReplace
- distinct, drop, estimatedDocumentCount, aggregate
The query operators covered are $regex with $options, $size, $type, $mod, $all and $not. The update operators are $addToSet, $pop, $mul, $min, $max and $currentDate. The aggregation pipeline supports $match, $project, $group and $unwind. Only the first-stage $match is pushed down to SQL. Later stages run in memory, which matters for large result sets.
The $regex operator is backed by a SQLite user-defined function. The codebase carries a ReDoS warning for this, so avoid untrusted patterns with nested quantifiers.
Anything outside that list should be treated as unverified until you have checked it against your own tests. Change streams also exist in the codebase, but check the repository before relying on them in production.
Should you test locally before deploying?
Reproduce the walkthrough against a plain local SQLite file first. Use the same collection names, the same filters and the same update operators, and confirm the results match what you expect from MongoDB. Once that passes, move the same code into a Durable Object without changing the query calls. Only the import, the adapter construction and the start-up block should differ.
After the move, compare timings against the local run. Run the same workload on both and record latency per call. Differences will show you where the Durable Object's storage path costs more than the file-backed one, and whether the in-memory stages of an aggregation are worth moving into a first-stage $match.
The source, tests and the example setup are in the mongolite repository on GitHub, if you want to read the adapter code alongside this walkthrough.