How to use MongoDB query syntax with SQLite backends

Developers building for the edge or browser often face a difficult trade-off. You may prefer the expressive power of the MongoDB API for your application logic, but your runtime environment, such as a browser, a serverless function or Cloudflare Durable Objects, needs the portability and simplicity of SQLite. MongoLite, published on npm as @semics-tech/mongolite (formerly mongolite-ts), bridges this gap by providing the MongoDB driver API over a SQLite backend.

Instead of rewriting your data layer into complex SQL, you can continue to use the collection API you already know. The library translates MongoDB-style queries, update operators, and aggregation pipelines into SQLite commands, allowing you to maintain a consistent data access pattern across different storage layers.

What is the current state of MongoDB parity in MongoLite?

Parity is the primary goal of the project, and it is verified through a parity test suite. Every pull request runs the same scenarios against MongoLite and a real MongoDB 7.0 instance, and fails if the results differ. As of version 1.7.0, the library supports a significant portion of the core MongoDB API.

The implementation focuses on the following functional areas:

  • Collection API: find, findOne, insertMany, replaceOne, findOneAndUpdate, findOneAndDelete, findOneAndReplace, distinct, drop, estimatedDocumentCount, and aggregate.
  • Query Operators: $regex (with $options), $size, $type, $mod, $all, $not and $elemMatch.
  • Update Operators: $addToSet, $pop, $mul, $min, $max, and $currentDate (supporting both date and timestamp types).
  • Aggregation Pipeline: $match, $project, $group, $unwind and $lookup (equality form).
  • Change Streams: watch() reports inserts, updates and deletes on a collection.

The following example demonstrates how to perform standard CRUD operations using the library:

import { MongoLite } from '@semics-tech/mongolite';

const client = new MongoLite('./app.sqlite');
await client.connect();
const users = client.collection('users');

await users.insertMany([
  { name: 'Alice', age: 30, tags: ['admin'] },
  { name: 'Bob', age: 25, tags: ['user'] }
]);

const result = await users.find({ age: { $gt: 20 } }).toArray();

How does the aggregation pipeline work with SQLite?

The aggregation engine in MongoLite is designed to balance performance with correctness. To maintain efficiency, the library pushes the initial $match stage directly into the underlying SQL query. This ensures that the database filters the dataset before the data is processed in memory.

Subsequent stages, such as $project, $group, and $unwind, are evaluated in memory to match MongoDB's behaviour. This hybrid approach allows you to run complex data transformations without sacrificing the ability to use SQLite as your storage engine.

const pipeline = [
  { $match: { status: 'active' } },
  { $group: { _id: '$category', total: { $sum: 1 } } },
  { $project: { category: '$_id', total: 1, _id: 0 } }
];

const stats = await users.aggregate(pipeline).toArray();

Why use a backend adapter model?

The architecture of MongoLite is built around the IDatabaseAdapter interface. This abstraction layer is what enables the library to run in diverse environments. By decoupling the core logic from the storage implementation, the library can support various backends without changing your application code.

Current adapters include:

  • SQLite file: Standard persistent storage for Node.js environments.
  • In-memory SQLite: Ideal for testing and ephemeral data processing.
  • CloudflareDurableObjectAdapter: Stores documents in a Durable Object's own SQLite storage. $regex is not supported on this adapter.
  • BrowserSqliteAdapter: Uses sql.js to provide a local-first database in the browser.

How can you contribute to the roadmap?

Releases are automated, and because parity is an ongoing effort, the roadmap is largely driven by what users need. If you find that a specific operator or aggregation stage is missing, the most effective way to see it prioritised is to open an issue on the MongoLite GitHub repository.

Every pull request that modifies query, projection, or aggregation behaviour must include a parity scenario that compares the output against a real MongoDB instance. This oracle-based testing keeps the library's behaviour checked as it grows.

To try it, run npm install @semics-tech/mongolite and test it against your own queries. The compatibility table lists every supported operator, and you can follow releases by starring semics-tech/mongolite on GitHub. If you hit a limitation, please open an issue.

Image