Filter + project essentials
Write a query to return active users only, showing name and email, sorted by newest signup date.
Data / Knowledge library
document-oriented nosql database for flexible apps
MongoDB helps teams store and query semi-structured data quickly without rigid table schemas, making it easier to ship features when data shapes evolve.
2009: MongoDB was first released by 10gen as a document database. 2013: 10gen rebranded as MongoDB, Inc. 2018: MongoDB 4.0 introduced multi-document ACID transactions (replica sets). 2019: MongoDB 4.2 extended transactions to sharded clusters. 2018 onward: SSPL licensing and rapid growth of Atlas as the managed default.
Work through these exercises in order. Try the task before opening a reference.
Write a query to return active users only, showing name and email, sorted by newest signup date.
For an e-commerce app, decide what to embed vs reference for orders, line items, and products. Justify in 4 bullet points.
Build an aggregation that returns total revenue per category for the last 30 days, then add an index plan to speed it up.
Create a collection validator that requires `email` and `createdAt`, and restricts `status` to `active|inactive`.
Given a slow query with `COLLSCAN`, identify one missing index and one query rewrite.
Global e-commerce retailers managing diverse product catalogs with rapidly changing attributes.
Relational databases force rigid schemas that require expensive migrations whenever a new product category, such as electronics with technical specs or apparel with sizing variants, is introduced to the catalog.
MongoDB uses a flexible document data model that allows developers to store non-uniform product data in a single collection, mapping directly to application objects without complex object-relational mapping.
This diagram illustrates how a central product collection handles diverse data structures through embedding.
Central storage for all inventory items
Contains technical specs array
Contains size and fabric nested objects
Maps JSON directly to code objects
This demonstrates how a document can store unique fields like 'specs' without needing a separate table for electronics.
db.products.insertOne({ sku: 'ELEC-001', name: 'Laptop', specs: { ram: '16GB', cpu: 'Octa-core' } }); // Embeds specific attributes directlyDevelopers can iterate on product catalog features and support new inventory types without downtime or schema migration overhead.
The flexibility of dynamic schemas can lead to data inconsistency if application-level validation is not strictly enforced; check for this by auditing document structures periodically.