MongoDB for SQL Developers
MongoDB stores data as JSON-like documents in collections. If you know SQL, most concepts map directly; the big shift is designing around how data is read rather than normalising everything.
Vocabulary
- Table → collection.
- Row → document (stored as BSON, which adds types like dates and ObjectIds).
- Column → field; documents in a collection can have different fields.
- Primary key →
_id, generated automatically if you don't set it. - JOIN → usually embedding related data, or
$lookupwhen needed.
CRUD with the Node.js driver
import { MongoClient } from 'mongodb';
const client = new MongoClient(process.env.MONGODB_URI);
const orders = client.db('shop').collection('orders');
// INSERT
await orders.insertOne({
customer: { id: 7, name: 'Asha' },
items: [{ sku: 'MUG-01', qty: 2, price: 349 }],
status: 'paid',
createdAt: new Date(),
});
// SELECT ... WHERE status = 'paid' AND total > 500 ORDER BY createdAt DESC LIMIT 10
const recent = await orders
.find({ status: 'paid', 'items.price': { $gt: 300 } })
.sort({ createdAt: -1 })
.limit(10)
.toArray();
// UPDATE
await orders.updateOne({ _id: orderId }, { $set: { status: 'shipped' }, $push: { history: { at: new Date(), status: 'shipped' } } });
// DELETE
await orders.deleteMany({ status: 'cancelled', createdAt: { $lt: new Date('2026-01-01') } });
Common query operators
- Comparison:
$eq,$ne,$gt,$gte,$lt,$in. - Logic:
$and,$or,$not. - Arrays:
$elemMatch,$all,$size. - Updates:
$set,$unset,$inc,$push,$pull,$addToSet.
When MongoDB fits well
- Data that naturally forms documents read together (a product with variants, an order with items).
- Rapidly evolving or varied structures.
- Horizontal scaling across shards for large datasets.
For highly relational data with many cross-entity queries and strict integrity rules, a relational database is often simpler.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.