MongoDB
MongoDB is a database that stores records as JSON-like documents. A document can contain fields, lists, and nested objects, so related details can be stored together.
Also known as: монга
How MongoDB organizes data
MongoDB is a document-oriented NoSQL database. It groups documents into collections. Think of a products collection: each document represents one item, with common fields such as name and price and category-specific details such as a monitor's screen size. A laptop in the same collection can have a different set of details.
MongoDB stores documents in BSON, a binary format with types beyond standard JSON, including dates and ObjectId values. In a regular collection, every document has a unique _id. A flexible schema means documents need not all have the same fields; it does not mean field names and types should vary at random. Schema validation can enforce rules when the application writes data.
From a query to an index
Suppose an online store needs a list of monitors under $250, sorted from cheapest to most expensive. If the store keeps prices as integer cents, the query in mongosh could look like this:
db.products.find(
{ category: "monitor", priceCents: { $lt: 25000 } },
{ _id: 0, name: 1, priceCents: 1 }
).sort({ priceCents: 1 })
The first argument filters documents; the second selects the fields to return. A compound index on { category: 1, priceCents: 1 } lets the database look up the category and scan its prices in order. Use explain("executionStats") to check which plan MongoDB chose and how many index keys and documents it examined. Indexes take up space and add work to writes, so build them for the queries the application actually runs.
find() is useful for retrieving records. If you need a summary, such as the number of in-stock products in each category, an aggregation pipeline can filter the documents and then group and count them.
When documents are a good fit
Product catalogs and content systems often fetch an item and its details together, making embedding a natural choice. Don't turn that into a rule that everything belongs in one document. A growing list of reviews is usually better kept in a separate collection with a product reference: you can page through the reviews independently, and each BSON document has a 16 MiB limit.
What interviewers look for
Expect to explain a modeling decision in terms of reads, writes, and data growth. An update to one document is atomic, even if it changes several fields. Multi-document transactions are available on replica sets and sharded clusters, but they add overhead. Also know the difference between replication and sharding: replication keeps copies to support failover, while sharding divides the data across shards to scale out.