MongoDB basics: understand BSON documents, transactions, and deployment
MongoDB stores data as BSON documents, groups documents into collections, and groups collections into databases. Documents in one collection do not have to share identical fields, but that does not mean MongoDB has no schema. In practice, you must decide how to validate data, which indexes match your queries, and whether one atomic document update is enough or several documents must change in one transaction.
MongoDB stores data as BSON documents, groups documents into collections, and groups collections into databases. Documents in one collection do not have to share identical fields, but that does not mean MongoDB has no schema. In practice, you must decide how to validate data, which indexes match your queries, and whether one atomic document update is enough or several documents must change in one transaction.
This introduction focuses on self-managed server concepts in MongoDB Server Manual 9.0 documentation checked on November 23, 2026. It does not cover Atlas management screens, pricing, or service-specific limits.
How is MongoDB data organized?
A document is MongoDB's basic record: a set of fields and values. Values can include strings and numbers as well as dates, arrays, and nested documents. MongoDB stores documents as BSON, a binary representation related to JSON that supports additional data types. A document's _id is unique and immutable within its collection. Documents and BSON
Documents belong to collections, and a database contains one or more collections. Applications send commands through a driver; for a self-managed deployment, tools such as mongosh can also run queries and management commands. The server process mongod handles database requests. Databases and Collections
Application or mongosh
│ commands and queries
▼
mongod server
└─ database
└─ collection
├─ BSON document (_id)
└─ BSON documentThis is a simplified logical hierarchy, not a deployment diagram. Replica sets and sharded clusters add nodes and routers.
Does a flexible schema mean there are no rules?
By default, documents in a collection may have different fields and types. That can help with catalogs whose product types have different attributes or with applications that evolve gradually. But an application that reads both older and newer documents must handle missing fields and changed types. Data Modeling
Flexibility does not prevent bad input. Schema validation can require fields, BSON types, or value ranges. For example, requiring start_date to be a date helps prevent strings that merely look like dates from entering the collection. By default, inserts and updates that fail validation are rejected. Decide whether your application, the database, or both should enforce the rules. Schema Validation
Start modeling with the data that a screen or API reads together. A document model can keep related values together, but shared values that change independently—such as current inventory—should not be copied into every order without a clear update strategy. Choose embedding or references based on access patterns, change frequency, and bounded size; this article focuses on how that choice interacts with MongoDB atomicity and queries.
How do single-document atomicity and transactions differ?
MongoDB writes are atomic at the single-document level, even when several fields change. For a stock decrement, including the expected current condition in the filter helps prevent an update based on stale data from overwriting a concurrent change. Atomicity and Concurrent Updates
db.inventory.updateOne(
{ _id: "SKU-42", quantity: { $gte: 1 } },
{ $inc: { quantity: -1 } }
)This illustrative command assumes the document exists and the field has the expected type; it was not run or tested against a server. The application should inspect the driver's result, such as matchedCount, to determine whether stock was actually decremented. Filtering only by _id can allow a later concurrent update to overwrite a value without enforcing the business condition.
If saving an order and decrementing inventory must succeed or fail together, single-document atomicity is not sufficient. MongoDB supports ACID transactions spanning documents, collections, databases, and shards; details depend on configured read and write concerns. Transactions can commit or roll back a group of operations, but may add coordination cost and operational complexity. First ask whether the data boundary can express the required consistency in one document; use a multi-document transaction when that atomicity is truly required. Transactions
What do indexes change for reads and writes?
Without a suitable index, finding matching documents can require scanning a large part of a collection. An appropriate index can narrow the documents MongoDB must examine. For example, if an application often filters orders by customerId and a createdAt range, review its actual filter and sort before considering a single-field or compound index. An index does not guarantee every query will be faster; inspect the query plan and work performed. Indexes · Query Plans
Indexes use storage and must be updated when documents are inserted or indexed fields change, which adds write cost. Choose indexes by weighing read frequency, write volume, and whether the planner uses them. For a slow query, inspect its shape, selectivity, sort, and plan; then compare the read improvement with the write overhead.
What problems do replication and sharding solve?
A replica set is a group of mongod processes that maintain the same data set. Typically, the primary receives writes and secondaries replicate changes. An eligible secondary can become primary if a member fails, improving redundancy and availability. Replication keeps copies across servers; it is not a substitute for a separate backup and restore process. Replica Set Fundamentals
Sharding distributes data and workload across machines to scale beyond a single server's practical capacity. A sharded cluster has shards (each deployed as a replica set), mongos routers that direct queries, and config servers that store cluster metadata. Shard-key choice and data distribution affect behavior, so adding servers alone does not guarantee improvement. Sharding also increases operational complexity; first decide whether one server or a replica set is sufficient. Sharding Architecture
How can you reason through a design?
Consider a hypothetical online shop. Store item names and prices as they were at purchase in the order document if the order screen reads them together and they should not change later. Keep current inventory as shared state in a separate document and conditionally decrement it. Use a transaction only if order creation and inventory decrement must be atomic together. Design indexes for the order lookup pattern, inspect query plans, and consider sharding only when data volume or throughput exceeds a single server's practical capacity. This is an illustrative scenario, not a benchmark or universal prescription.
Before applying a design:
Record which data the application reads and changes together.
Check that application code and validation rules handle differences among documents.
Decide whether changes fit one atomic document or truly need cross-document atomicity.
Choose indexes for representative filters and sorts, then review both query plans and write costs.
Plan replication for availability and sharding for data distribution, while treating backup and restore as separate work.
Document size, index behavior, transaction settings, and deployment limits depend on version and environment. Check the official documentation for the target deployment and validate with safe test data and representative queries. Code and examples here are illustrative and were not run against MongoDB.
Scope and official sources
The MongoDB Manual pages below were checked on November 23, 2026. This guide explains the server document model and self-managed deployment concepts; it does not cover Atlas-only interfaces or limits.
[Documents](https://www.mongodb.com/docs/manual/core/document/)
[Databases and Collections](https://www.mongodb.com/docs/manual/core/databases-and-collections/)
[Data Modeling](https://www.mongodb.com/docs/manual/data-modeling/)
[Schema Validation](https://www.mongodb.com/docs/manual/core/schema-validation/)
[Atomicity and Transactions](https://www.mongodb.com/docs/manual/core/write-operations-atomicity/)
[Transactions](https://www.mongodb.com/docs/manual/core/transactions/)
[Indexes](https://www.mongodb.com/docs/manual/indexes/)
[Query Plans](https://www.mongodb.com/docs/manual/core/query-plans/)
[Replication and Replica Set Fundamentals](https://www.mongodb.com/docs/manual/replication/)
[Sharding Introduction and Architecture](https://www.mongodb.com/docs/manual/sharding/)
[MongoDB Limits and Thresholds](https://www.mongodb.com/docs/manual/reference/limits/)