MongoDB is a document database. It stores data as JSON-like documents, kept in a binary format called BSON, inside collections instead of rows inside tables. Documents in one collection can have different fields, and related data can be nested in one document. It suits nested, changing data and is a weaker fit when many tables must be joined.
MongoDB turns up in almost every MERN project and in many backend interviews, yet most explanations stay vague or jump straight to commands. This page gives the idea first, then the commands, then the limits you will hit on the free cluster.
What MongoDB does and the limits of its free Atlas cluster were read from MongoDB's own documentation and pricing page on 11 November 2026. The Python example is a toy that imitates query documents. It is not MongoDB and needs no server.
MongoDB in one picture
A relational database such as MySQL keeps data in tables of rows and columns. MongoDB keeps data in documents: field and value pairs, much like a JSON object, where a value can be a number, text, a date, an array or another document. A group of documents is a collection, the nearest thing to a table. MongoDB's documentation calls the document the basic unit of data.
Documents in one collection do not have to share the same fields, and related data can sit inside the document instead of in another table. The student document below holds what a relational design would spread over three tables (students, skills and projects) joined by IDs.
{
name: 'Asha Rao',
city: 'Pune',
skills: ['python', 'sql'],
projects: [
{ title: 'Expense tracker', stack: 'MERN' },
{ title: 'Quiz app', stack: 'Flutter' }
]
}MongoDB stores documents as BSON, a binary representation of JSON that adds types JSON lacks, such as dates and 64-bit integers. Every document has an _id field that acts as the primary key: it must be unique in the collection and cannot be changed. One document can be at most 16 mebibytes, and nesting is limited to 100 levels.
Flexible does not mean no rules. MongoDB's schema validation lets you require field types and value ranges once your data settles down, for example a GPA field that must be a positive number.
MongoDB vs a SQL database
| Idea | SQL database (MySQL, PostgreSQL) | MongoDB |
|---|---|---|
| One record | Row | Document |
| A group of records | Table | Collection |
| Shape of the data | Fixed columns, set up first | Flexible, with optional validation rules |
| Related data | Separate tables joined by foreign keys | Often embedded in one document, or stored apart and linked by ID |
| Query language | SQL | Query documents and the aggregation pipeline, run from mongosh or a driver |
| Transactions | ACID transactions across tables | A single document is atomic; multi-document ACID transactions are also supported |
The idea that MongoDB has no transactions is out of date. Its documentation says MongoDB supports multi-document ACID transactions, but it also says embedding related data in one document reduces the need for them. For the SQL side see SQL vs MySQL, and for the other kinds of database see types of databases.
How common is it? In the Stack Overflow Developer Survey 2026, 19.0% of the 12,061 people who answered the database question said they had done extensive development work with MongoDB in the past year. PostgreSQL scored 57.9%, SQLite 42.1% and MySQL 35.1%. MongoDB is widely used, but SQL databases reach more people, so learn SQL as well.
CRUD and indexes in mongosh
CRUD means create, read, update and delete. In mongosh, the MongoDB shell, each one is a method on a collection. These commands are for a real MongoDB server or an Atlas cluster. They are checked against the documentation but not run on this page.
use campus
db.students.insertMany([
{ name: 'Asha', city: 'Pune', skills: ['python', 'sql'], cgpa: 8.4 },
{ name: 'Ravi', city: 'Delhi', skills: ['java'], cgpa: 7.1 },
{ name: 'Meera', city: 'Pune', skills: ['python', 'react', 'mongodb'], cgpa: 9.0 }
])
db.students.find({ city: 'Pune' })
db.students.find({ skills: 'python', cgpa: { $gt: 8.5 } }, { name: 1, _id: 0 })
db.students.updateOne({ name: 'Ravi' }, { $set: { city: 'Mumbai' }, $push: { skills: 'sql' } })
db.students.deleteOne({ name: 'Ravi' })insertManycreates documents. UseinsertOnefor a single one.find(filter, projection)reads. The filter is itself a document:{ skills: 'python' }matches every student whose skills array contains python, and{ cgpa: { $gt: 8.5 } }uses the greater-than operator. The second argument picks which fields come back.updateOne(filter, update)changes the first match. Operators describe the change:$setsets a field and$pushappends to an array.deleteOne(filter)removes the first match.
From Python the same ideas look like this with the pymongo driver. Keep the connection string in an environment variable and never commit it to GitHub; the security checklist covers leaked keys.
import os
from pymongo import MongoClient
client = MongoClient(os.environ["MONGODB_URI"]) # the connection string from the Atlas dashboard
students = client["campus"]["students"]
students.insert_one({"name": "Asha", "skills": ["python", "sql"]})
print(students.find_one({"skills": "sql"})["name"])A toy that shows what a query document means
This is 27 lines of plain Python with no storage and no indexes. It supports only equality, array membership, $gt and $in. A real server supports many more operators. It exists to show that a MongoDB filter is just data that describes the documents you want.
students = [
{"name": "Asha", "city": "Pune", "skills": ["python", "sql"], "cgpa": 8.4},
{"name": "Ravi", "city": "Delhi", "skills": ["java"], "cgpa": 7.1},
{"name": "Meera", "city": "Pune", "skills": ["python", "react", "mongodb"], "cgpa": 9.0},
]
def matches(doc, query):
for field, cond in query.items():
value = doc.get(field)
if isinstance(cond, dict): # operator form, like {"$gt": 8.5}
for op, arg in cond.items():
if op == "$gt" and not (value is not None and value > arg):
return False
if op == "$in" and value not in arg:
return False
elif isinstance(value, list): # an array field matches if it contains the value
if cond not in value:
return False
elif value != cond:
return False
return True
def find(collection, query):
return [doc["name"] for doc in collection if matches(doc, query)]
print(find(students, {"city": "Pune"}))
print(find(students, {"skills": "python", "cgpa": {"$gt": 8.5}}))
print(find(students, {"city": {"$in": ["Delhi", "Mumbai"]}}))Output: ['Asha', 'Meera'], then ['Meera'], then ['Ravi']. The second filter matches Meera only because her skills array contains python and her cgpa is above 8.5.
Indexes: why one query is fast and another is slow
Without an index MongoDB checks every document in a collection to answer a query. An index is a sorted structure on one or more fields that lets it jump to the matches. MongoDB's own example is an index on a gpa field, which speeds up exact matches and range queries on that field.
db.students.createIndex({ cgpa: 1 })
db.students.find({ cgpa: { $gt: 8.5 } }).explain('executionStats')explain() shows the plan MongoDB chose. In its output a COLLSCAN stage means a scan of the whole collection and an IXSCAN stage means an index was used. If a frequent query shows COLLSCAN, an index on its filter field is the first thing to try.
Indexes cost space and make writes a little slower. On the Atlas free cluster they also use your storage allowance, because the storage limit covers documents plus their indexes.
When to use MongoDB, and when not to
Good fits
- Data that is nested or varies between records: product catalogues, user profiles, content pages and event logs.
- A project where the data shape is still changing and the whole stack is JavaScript, as in a MERN app (see tech stacks explained).
- Reading a whole object, such as a student with all projects, in one request.
Weak fits
- Data full of many-to-many relationships that you report on with many joins. A relational database is simpler.
- Tabular data in a team that already knows SQL. DB-Engines lists MySQL and PostgreSQL as multi-model systems that also offer a document model.
- Projects where nobody has decided what the data looks like. Flexibility without validation turns into inconsistent records.
A common student mistake is to pick MongoDB because it sounds modern and then rebuild joins by hand in code. Choose by the shape of the data, not by fashion.
MongoDB Atlas free cluster: limits as of 11 November 2026
Atlas is MongoDB's hosted service. Its free tier used to be called M0, and the documentation now calls it a Free cluster. MongoDB's pricing page lists M0 at 512 MB and free forever. These limits come from the Atlas Free Cluster Limits page:
| Limit | Free cluster |
|---|---|
| Data storage | 0.5 GB, counting uncompressed BSON documents and their indexes |
| Throughput | 100 read and write operations per second; above that Atlas throttles the cluster |
| Connections | At most 500 |
| Databases and collections | 100 databases and 500 collections in total |
| Data transfer | 10 GB in and 10 GB out in any rolling seven days |
| Clusters | One free cluster per project, a 3-node replica set, no sharding |
| MongoDB version | 8.0, and you cannot upgrade it yourself |
| Idle clusters | Paused after 30 days with zero connections; resumable unless it runs a version Atlas can no longer restore |
| Backups | Optional: one daily snapshot, up to four kept |
| Aggregation | Pipelines of up to 50 stages; no temporary files written to disk |
For a learning project this is plenty. The catches are storage and speed: 512 MB fills quickly if you put images inside documents (store files elsewhere and keep only the URL in MongoDB), and 100 operations per second can bite if a demo gets traffic. MongoDB's own advice is to export a copy of your data if you will not use a free cluster for a long time.
You can also run the self-managed Community edition, which MongoDB's documentation describes as source-available and free to use. To compare with Postgres options see free backends and databases, and for student credits see free cloud credits.
MongoDB interview questions with short answers
| Question | Short answer |
|---|---|
| What is BSON? | A binary format for documents. It is JSON plus extra types such as dates and 64-bit integers. |
| Embedding or referencing? | Embedding nests related data inside one document, so one read returns everything. Referencing keeps related data in another collection and links by ID. It suits data shared by many documents or data that grows without limit, since one document is capped at 16 MiB. |
| What does _id do? | It is the primary key: unique in the collection and immutable. |
| What is an index and what does it cost? | A sorted structure that speeds up reads on a field. It uses storage and slows writes slightly. |
| What is the aggregation pipeline? | A series of stages, such as $match and $group. The output of each stage feeds the next. |
| Does MongoDB support transactions? | Yes, multi-document ACID transactions. A single-document operation is atomic anyway. |
| What is a replica set? | A group of servers holding copies of the same data so another can take over if one fails. The free Atlas cluster runs three nodes. |
| SQL or NoSQL? | NoSQL: it is not relational, although it has transactions and a rich query language of its own. |
Put the idea to work in a MERN build from full-stack projects with source code or a backend project. Masai's longer guide: What is MongoDB? Explained in 5 minutes.
Frequently asked questions
What is MongoDB used for?
Applications whose data is nested or changes shape: product catalogues, user profiles, content, logs and real-time apps. It is the M in the MERN and MEAN stacks. Reading one document returns an object with its nested data, so many queries need no join.
Is MongoDB SQL or NoSQL?
NoSQL. It is a document database, not a relational one, and it does not use SQL as its main language. It does support multi-document ACID transactions and optional schema validation, so it is not the loose, rule-free store that early descriptions of NoSQL suggest.
Is MongoDB free?
Yes, in two ways. The self-managed Community edition is source-available and free to use, and the hosted Atlas free cluster gives 0.5 GB of storage and 100 operations per second as of 11 November 2026. Larger Atlas clusters are paid, so check the pricing page before you scale up.
What is the difference between MongoDB and MySQL?
MySQL stores rows in tables with fixed columns and is queried with SQL. MongoDB stores flexible documents in collections and is queried with query documents. Both can be used for web apps; choose by the shape of your data and what your team knows.
What is BSON in MongoDB?
BSON is the binary format MongoDB uses to store documents and send them between the server and your code. It looks like JSON but adds data types such as dates and 64-bit integers, and a single BSON document can be at most 16 mebibytes in size.
Should I learn MongoDB or SQL first?
SQL first. It was the second most used language in the Stack Overflow Developer Survey 2026 at 58.4%, and the ideas transfer to every relational database. Learn MongoDB next, ideally while building a small MERN project, so the commands stick.
Does MongoDB have a schema?
Not by default: documents in one collection can have different fields. Once the structure of your data is settled, you can add schema validation rules for field types and value ranges, so bad inserts are rejected instead of quietly stored.
Sources checked
- Documents, BSON, _id rules
- 16 MiB document limit, 100 nesting levels
- Atlas Free cluster limits
- M0 512 MB free forever
- multi-document ACID transactions
- flexible schema and validation
- COLLSCAN and IXSCAN
- database usage, n = 12,061
- model classification and ranking, Nov 2026
Learn this with mentors
Programmes for builders and engineers with admissions open: live classes, mentors and project work.


