Masdriv is a campaign-sending app (FastAPI + Motor/PyMongo + Celery + Redis) that kept everything in MongoDB. One day we decided to find out whether we could swap the database for NonoDB — and it turned out that after a handful of code changes the whole migration went smoothly: the application code stayed almost untouched, the data moved in minutes, and the stack now runs on NonoDB in production.
This article covers why we did it, how to do it step by step, and what we got — with concrete numbers.
Why change the database at all?
MongoDB is great, but for an app of our size (a few thousand documents, one server, no sharding) we were paying for complexity we never used:
- a replica set or standalone plus all the operational tooling (
mongosh, dumps, background index builds), -
mongodumpbackups → 510 MB on disk, - a WiredTiger cache that by default reserves a large slice of the host's RAM,
- backups that need writes paused, or fiddling with
--oplog.
NonoDB promises something different: one process, one binary, data in LMDB, and a MongoDB wire listener on top. That last part was the key — the app keeps speaking "MongoDB", and we only change the host in MONGO_URI.
Is it a drop-in?
Almost. pymongo and motor work unchanged, tests on mongomock work unchanged, Celery works unchanged. In practice the whole switch is:
DB_BACKEND=nonodb MONGO_URI=mongodb://admin:***@nonodb:27017/masdriv?authSource=admin Along the way we had to handle three things where NonoDB (still) differs from MongoDB.
1. No sparse and no TTL indexes
NonoDB supports neither the sparse option nor expireAfterSeconds. The fix turned out to be simple:
-
sparse→ a plain unique index. The semantics are effectively the same: a missing field does not collide, only an explicitnulldoes (and the app never writesnullthere). - TTL → a
_ttlfield (in seconds) set at insert time. NonoDB strips it before storing and expires the document in the background.
# database.py IS_NONODB = os.getenv("DB_BACKEND", "mongodb").lower() == "nonodb" def _unique_opts(sparse: bool = True) -> dict: return {} if IS_NONODB else ({"sparse": True} if sparse else {}) def with_ttl(doc: dict, seconds: int) -> dict: """NonoDB: retention via a `_ttl` field instead of a TTL index.""" if IS_NONODB and seconds > 0: doc["_ttl"] = int(seconds) return doc The backend picks its behaviour from a single environment variable, so the code behaves identically on both databases — which let us compare results 1:1.
2. No $toString in aggregations
We had two $lookup stages comparing an _id (ObjectId) with an ID stored in a queue (string) via $toString. NonoDB doesn't have it — and on older versions such a $lookup silently returned empty results. Instead of fighting the pipeline, we solved it in Python: one $in query per collection plus an in-memory map.
async def names_by_id(collection, ids, field: str) -> dict: wanted = {str(v) for v in ids if v is not None} object_ids = {k: ObjectId(k) for k in wanted if ObjectId.is_valid(k)} out = {} if object_ids: async for doc in collection.find({"_id": {"$in": list(object_ids.values())}}, {field: 1}): out[str(doc["_id"])] = doc.get(field) missing = [k for k in wanted if k not in out] if missing: async for doc in collection.find({"_id": {"$in": missing}}, {field: 1}): out[str(doc["_id"])] = doc.get(field) return out The result: fewer round-trips than $lookup per row, and identical output on both databases.
3. Healthcheck and authentication
mongosh --eval "db.runCommand('ping')" became nonodb-cli ping, and SCRAM is configured with NONODB_MONGO_USERNAME / NONODB_MONGO_PASSWORD — the same credentials used in MONGO_URI.
Migrating the data
This is where NonoDB positively surprised us: both databases speak the same protocol, so we needed no mongodump/mongoimport and no intermediate files. A pymongo script that copies documents in batches (upsert by _id) was enough, followed by a comparison of document counts and a content checksum for every collection.
# 1. dry run python3 migrate_mongo_to_nonodb.py \ --source "$MONGO_URI" \ --target "mongodb://admin:***@nonodb:27017/masdriv?authSource=admin" --dry-run # 2. copy (idempotent — safe to re-run) python3 migrate_mongo_to_nonodb.py --source "$MONGO_URI" --target "$NONODB_URI" # 3. verification only: count + sha256(content) per collection python3 migrate_mongo_to_nonodb.py --source "$MONGO_URI" --target "$NONODB_URI" --verify-only The result for our database: 33 collections, 6,435 documents, zero differences in verification. BSON types (ObjectId, dates, Decimal128) came through without conversion, indexes rebuilt themselves from list_indexes(), and the only things that had to be skipped were sparse/TTL — with a clear message saying so.
The whole process took under a minute, verification included.
Testing before and after
Before switching production traffic we ran:
- 265 backend tests (
pytest,mongomock) — green, unchanged, - a protocol conformance test (22 cases: CRUD, cursors, indexes,
$lookup,$facet,$group, upsert, BSON types) — 22/22, - a differential test: the same app against MongoDB and NonoDB, same data, 88 GET endpoints and 56 write steps — 0 differences in status codes and JSON shape,
- the send pipeline (200 messages): identical 200
sent, 200attempts, 200 events, 200 idempotency keys.
Only then did we move traffic.
What we gained
Resources
| MongoDB | NonoDB | |
|---|---|---|
| Disk (6,435 docs, 33 collections) | 510 MB (data + journal) | 9.3 MB |
| Database process RAM | WiredTiger cache reserves a large share of host RAM | 15–30 MiB RSS |
| Idle CPU | process + WT threads | ~0% |
| Backup | 510 MB dump, needs consistency handling | 656 KB (gzip), online |
The LMDB map is reserved at 1 GiB (it can grow online via nonodb-cli resize) but only 9.7 MB is actually used — the rest is empty, sparse space. That leaves room on the VPS we could previously only dream about.
New capabilities
This isn't just "a lighter MongoDB" — NonoDB gives us things we simply didn't have before:
- A JSON API and SQL next to the MongoDB protocol:
{"op":"find","coll":"users","filter":{...}}or a plainSELECT— great for diagnostics and analytics withoutmongosh. -
watchand subscriptions — a change stream over SSE, i.e. a natural outbox for webhooks and notifications. - Document history (
history,get_version) andrestore --to <timestamp>— point-in-time recovery, which MongoDB won't do with a single command. - Shadow mode — the daemon also forwards writes to MongoDB and records diffs as JSONL. The simplest possible way to validate a migration against real traffic.
- Backups with a manifest and SHA-256,
verify-backup, retention, and S3 upload. - Built-in vector, fuzzy and full-text indexes — a replacement for a good chunk of Atlas Search without extra infrastructure.
- A single data file and zero replication — deployment is one container and one volume.
How the maintainers responded
During testing we found a few things that didn't behave ideally (sorting null differently from MongoDB, insert_many committing per document, slow $in on large lists). We filed them as issues — and all three were fixed within hours, with regression tests. After building from main, the problems were gone: insert_many went from 550 ms to 32 ms for 1,000 documents, $in with 500 values from 1,230 ms to 10 ms, and null sorting now matches MongoDB exactly.
For a 0.x project that's a very good sign: the author responds faster than many teams inside companies do.
Things to watch out for
To keep it honest, here is what you should check before migrating:
- Indexes: review
list_indexes()forsparse,partialFilterExpression,collation— those options are not supported. - Aggregations: check for
$toString/$convertinside expressions; if present, move that logic into the application. - Large
$inlists and bulk inserts: on 0.29.1 these were slower than MongoDB (fixed onmain); measure your own pattern before cutover. - Sorting on fields that can be
null— check whether ordering matters for your pagination. - Transactions, sessions, change streams — if you rely on them, NonoDB doesn't have them yet.
Wrapping up
Migrating from MongoDB to NonoDB turned out to be much easier than we assumed. The key was that NonoDB speaks the MongoDB protocol: we didn't rewrite queries, we didn't change drivers or architecture. Three small changes in one module (database.py) and one endpoint, plus a 250-line migration script.
In return we got a database that takes 9 MB instead of 510 MB, uses a few dozen megabytes of RAM instead of a cache reservation, backs up online in under a second — and, on top of that, offers features we never had: document history with point-in-time restore, change streams, SQL and vector indexes.
If you run an app whose database workload is mostly CRUD plus a few aggregations, NonoDB is worth a look. Start with shadow mode, migrate a copy of your data, and run a differential test. For us it took one day and not a single sleepless night.
