DEV Community

Alex Spinov
Alex Spinov

Posted on

FerretDB Has a Free API You've Never Heard Of

FerretDB is an open-source MongoDB alternative that uses PostgreSQL or SQLite as its storage backend. It speaks the MongoDB wire protocol, so your existing MongoDB drivers, tools, and applications work unchanged — but your data lives in Postgres.

What Makes FerretDB Special?

  • MongoDB-compatible — same wire protocol, same drivers
  • Postgres backend — your data in a proven RDBMS
  • No vendor lock-in — escape MongoDB licensing
  • SQLite mode — embedded document database
  • Free — Apache 2.0 license

The Hidden API: MongoDB Protocol on Postgres

import { MongoClient } from 'mongodb';

// Connect to FerretDB — same MongoDB driver!
const client = new MongoClient('mongodb://localhost:27017/mydb');
const db = client.db('mydb');

// Standard MongoDB operations — all work!
await db.collection('users').insertOne({
  name: 'Alice',
  email: 'alice@example.com',
  age: 30,
  tags: ['developer', 'rust']
});

// Queries
const developers = await db.collection('users')
  .find({ tags: 'developer', age: { $gte: 25 } })
  .sort({ name: 1 })
  .limit(20)
  .toArray();

// Aggregation pipeline
const stats = await db.collection('orders').aggregate([
  { $match: { status: 'completed' } },
  { $group: { _id: '$category', total: { $sum: '$amount' }, count: { $sum: 1 } } },
  { $sort: { total: -1 } }
]).toArray();

// Indexes
await db.collection('users').createIndex({ email: 1 }, { unique: true });
Enter fullscreen mode Exit fullscreen mode

But your data is in Postgres!

-- Connect to the underlying Postgres
-- and query with SQL too!
SELECT * FROM mydb.users 
WHERE _jsonb->>'name' = 'Alice';

-- Use Postgres features on your document data
SELECT _jsonb->>'category' as category,
       COUNT(*) as count,
       SUM((_jsonb->>'amount')::numeric) as total
FROM mydb.orders
WHERE _jsonb->>'status' = 'completed'
GROUP BY _jsonb->>'category';
Enter fullscreen mode Exit fullscreen mode

Quick Start

# Docker
docker run -p 27017:27017 ghcr.io/ferretdb/ferretdb \
  --postgresql-url=postgres://user:pass@host:5432/ferretdb

# Or with built-in SQLite
docker run -p 27017:27017 ghcr.io/ferretdb/ferretdb \
  --handler=sqlite
Enter fullscreen mode Exit fullscreen mode

Why Teams Choose FerretDB

A CTO shared: "MongoDB changed their license, and we couldn't accept SSPL for our SaaS. FerretDB let us swap the backend to Postgres without changing a single line of application code. Our team already knows Postgres, so we got better backups, monitoring, and ACID transactions for free."


Need database migration tools? Email spinov001@gmail.com or check my solutions.

MongoDB or Postgres? Why not both with FerretDB?

Top comments (0)