Skip to content

Guide to Transitioning from SQL to MongoDB for SQL Professionals

Gulab Chand Tejwani

Gulab Chand Tejwani

PMP · PL-300 · Microsoft Certified Trainer

· 6 min read

Blog
Guide to Transitioning from SQL to MongoDB for SQL Professionals

Moving from SQL to MongoDB means changing how you model data more than learning new syntax. In a relational database you normalise data into tables and join them at query time. In MongoDB you store related data together in JSON-like documents, shaped around how your application reads it, and use embedding or references instead of joins. Your SQL knowledge still helps: filters, indexes, aggregation and query plans all have direct MongoDB equivalents.

I teach SQL to analysts and developers, and when developers in our classes start a MongoDB project, the hard part is rarely the query language. It's resisting the urge to recreate every table as a collection.

How is MongoDB different from a relational database?

  • Tables become collections, rows become documents. Documents are stored as BSON (binary JSON) and can contain nested objects and arrays.
  • Schema is flexible. Documents in one collection don't have to share the same fields, though you can enforce rules with JSON Schema validation.
  • Joins are optional. You can embed related data in one document, or reference it and use the $lookup aggregation stage when needed.
  • Transactions exist. MongoDB has supported multi-document ACID transactions since version 4.0 (2018) on replica sets, and on sharded clusters since 4.2. Single-document writes are always atomic, which is one reason embedding works well.
  • Scaling out is built in through sharding across servers.

How do SQL queries translate to MongoDB?

A filter:

SELECT * FROM users WHERE age > 30;
db.users.find({ age: { $gt: 30 } });

Selecting specific columns (a projection):

SELECT name, email FROM users WHERE city = 'Anand';
db.users.find({ city: "Anand" }, { name: 1, email: 1, _id: 0 });

Counting and grouping use the aggregation pipeline:

SELECT COUNT(*) FROM users WHERE age > 30;
db.users.aggregate([
  { $match: { age: { $gt: 30 } } },
  { $count: "totalUsers" }
]);
SELECT city, COUNT(*) AS users FROM users GROUP BY city ORDER BY users DESC;
db.users.aggregate([
  { $group: { _id: "$city", users: { $sum: 1 } } },
  { $sort: { users: -1 } }
]);

Think of the pipeline as a sequence of steps, each taking the previous step's output: $match is WHERE, $group is GROUP BY, $sort is ORDER BY, $project is the SELECT list, and $lookup is a left outer join.

How do you redesign a SQL schema for MongoDB?

Start from your application's queries, not from your tables. For each screen or API endpoint, ask what data it reads together.

Embed when the child data belongs to the parent and is read with it, for example an order and its line items, or a customer and their addresses. One read returns everything, and updates are atomic.

Reference when the related data is large, shared by many parents, or grows without limit, for example products referenced by many orders, or an activity log. Store the referenced _id and look it up when needed.

Watch the 16 MB maximum document size. Arrays that keep growing forever (every comment, every event) belong in their own collection.

What about schema changes?

In SQL you'd run:

ALTER TABLE Customers ADD membershipStatus VARCHAR(255);

In MongoDB new documents can simply include the field, and you can backfill existing ones:

db.customers.updateMany(
  { membershipStatus: { $exists: false } },
  { $set: { membershipStatus: "active" } }
);

Flexible doesn't mean unmanaged. Add JSON Schema validation to collections that matter, so bad data is rejected at write time.

How do indexes work in MongoDB?

Much like in SQL. Create indexes on fields you filter and sort by:

db.users.createIndex({ email: 1 }, { unique: true });
db.orders.createIndex({ customerId: 1, orderDate: -1 });

Compound index order matters, following the same equality, sort, range thinking you'd use in SQL Server. Check a query's plan with explain:

db.users.find({ age: { $gt: 30 } }).explain("executionStats");

Look for COLLSCAN (a full collection scan, like a table scan) on queries that should use an index.

Which tools help with a SQL to MongoDB migration?

  • MongoDB Relational Migrator: MongoDB's own tool for mapping a relational schema to documents and migrating data.
  • MongoDB Compass: a GUI for browsing data, building aggregation pipelines and viewing query plans.
  • mongosh: the MongoDB shell for scripts and quick queries.
  • MongoDB Atlas: the managed cloud service, with built-in monitoring and a Performance Advisor that suggests indexes.
  • Studio 3T: a third-party IDE that includes SQL-style querying and import from relational databases.

How should you plan the migration?

  1. List the application's main read and write patterns.
  2. Design documents for those patterns, and prototype with realistic data volumes.
  3. Migrate one bounded area at a time rather than everything at once.
  4. Run old and new in parallel where you can, and compare results.
  5. Keep a rollback plan until the new system has run cleanly in production.

When should you not move to MongoDB?

If your data is highly relational, reporting relies on complex joins across many entities, or your team and BI tools (such as Power BI) are built around SQL, a relational database may remain the better fit. Many systems use both: SQL for transactions and reporting, MongoDB for flexible or high-volume document data.

To strengthen the SQL side first, see why SQL matters and our SQL training. If you're building the application layer in .NET, our Blazor production checklist is a useful next read.

FAQ

Is MongoDB better than SQL?

Neither is better in general. MongoDB suits flexible, document-shaped data and horizontal scaling. Relational databases suit highly structured data, complex joins and reporting.

Does MongoDB support joins?

Yes, through the $lookup aggregation stage, which works like a left outer join. Good MongoDB designs often embed related data instead of joining.

Does MongoDB support transactions?

Yes. Multi-document ACID transactions have been available since MongoDB 4.0 on replica sets and 4.2 on sharded clusters.

Can I use SQL to query MongoDB?

Not natively in the core query language, but tools such as Atlas SQL interface, BI connectors and Studio 3T let you run SQL-style queries against MongoDB data.

How long does it take a SQL developer to learn MongoDB?

The query basics take a few days. Learning to design good document schemas usually takes a few real projects.

Choose optional cookies

We use necessary cookies for account security. With your permission, we also use analytics and advertising cookies on marketing pages. See our Privacy Policy.