MongoDB Practice Problems
Execute MQL and aggregation challenges. Validate predicates, accumulators, and output tuples directly against in-memory BSON collections.
Filter Active Developers
Write an MQL query to find all users where role is "developer" and status is "active".
2 documents (Bob Martinez, George Clark)
All 50 Practice Challenges
Filter Active Developers
Write an MQL query to find all users where role is "developer" and status is "active".
High-Value Orders Filter
Find all orders where total is strictly greater than 500.
Project Customer Names & Credit
Find all customers with tier = "Enterprise", projecting only the name and creditLimit fields (suppress _id).
Low-Stock Inventory Scan
Select all products whose current stock is less than 20 units.
Audio Hardware Catalog
Find all products in category "Audio".
US Infrastructure Nodes
Query all servers provisioned in region "us-east".
Pending Orders Audit
Find all orders where status is either "shipped" or "cancelled".
Nested Contact Email Filter
Find all customers whose nested contact.email ends with ".com" or matches domain acme.com.
High-Density Compute Fleet
Select all servers with 32 or more CPU cores (cores >= 32).
Canadian & German Customers
Find all customers residing in country "CA" or "DE".
Active High-Rated Products
Find all products that are active (active = true) and have a rating >= 4.7.
Senior Age Filter
Find all users with age strictly greater than 30.
Wire Payment Transactions
Find all orders where payment method is "wire".
Maintenance Fleet Nodes
Select all servers whose status is NOT "active".
Customer Credit Directory
Select all customer accounts with creditLimit >= 50000.
Array Tag Membership Filter
Find all users who have the tag "backend" inside their tags array.
Sorted & Limited Price Search
Find all active products, sorted by price in descending order (-1), limiting the result to the top 3 most expensive items.
Multiple Array Tags ($all)
Find all servers that have BOTH "k8s" AND "api" tags in their tags array.
Count Completed Orders by Category
Run an aggregation pipeline on orders to filter status = "completed" and count the number of orders in each category.
Total Revenue by Payment Method
Aggregate orders to compute total revenue ($sum of total) grouped by payment method, sorted descending by totalRevenue.
Customer Lifetime Spend by Tier
Aggregate customers where active = true, grouping by tier to find the average credit limit (avgCredit) rounded or calculated via $avg.
Nested Hardware Specification Filter
Find all products where the nested subdocument specs.ram is greater than or equal to 32.
Pagination with Skip and Limit
Sort all users by score descending (-1), skip the first 2 users, and limit the result to 3 users.
Unwind Tags and Count Frequency
Deconstruct the users tags array using $unwind, and count how many users have each individual tag.
Average Order Value by User
Group all orders by user_id to compute the total spend (totalSpent) and average order value (avgTotal).
Server Core Density Analysis
Group servers by region, calculating the total number of CPU cores (totalCores) and total RAM (totalRam) per region.
Project Computed Value with $project
Aggregate products projecting only the product name and a computed field inventoryValue (price multiplied by stock). Suppress _id.
Multiple Criteria Exclusion with $nor
Find all users where status is NOT "pending" AND country is NOT "DE".
Unique Product Categories Set
Use a single $group stage with _id: null to extract a deduplicated array of all product categories using $addToSet.
Order Quantity Minimum and Maximum
Group orders by category, finding the minimum total (minTotal) and maximum total (maxTotal) in each category.
Left Outer Join Orders with Customers
Execute an aggregation on orders joining the users collection on user_id = u._id using $lookup, outputting the user details in a field named customerDetails.
Top Spenders Ranking Pipeline
Aggregate orders where status = "completed", group by user_id to compute totalSpent, sort by totalSpent descending, and limit to top 2 spenders.
Filtered Array Projection with $elemMatch
Find all users who possess an array element matching a tag ending in "end" (backend or frontend).
Filter Join Results ($unwind + $match)
Join orders with users, unwind customer, and retain only transactions where the customer's country is "CA".
Multi-Condition Complex Filter
Find all orders where total > 200 AND category is NOT "Hardware" AND status = "completed".
Global Hardware Load Average Analysis
Filter active servers, and compute the global average loadAvg (avgLoad) and max loadAvg (peakLoad) across all active nodes.
Conditional Category Flagging ($cond)
Use $project or $set with $cond to project products with an added flag isExpensive (true if price >= 500, else false).
Top Product Per Category Discovery
Sort products by price descending, then group by category using $first to select the most expensive product in each category.
Pushing Order IDs by Customer
Group orders by user_id, pushing all order _id values into an array named orderHistory, along with orderCount.
Regional Compute Fleet Load Ranking
Group servers by region, finding total compute resources and sorting by total cores descending.
Active High-Capacity Enterprise Search
Find active Enterprise customers located in US or UK with credit limit greater than 60,000.
Multi-Role User Search
Find all users who are either an "admin" OR have a score strictly greater than 90.
Multi-Faceted Dashboard Aggregation ($facet)
Run an aggregation on orders using $facet to calculate both: 1. "byStatus": total count grouped by status, and 2. "overview": overall total revenue across all orders.
Enterprise Customer Order Value Audit
Join customers with orders, filter for customers where tier = "Enterprise", unwind orders, and compute the total spend per enterprise customer.
Price Range Bucketing ($bucket)
Partition products into three price tiers using $bucket: [0, 200), [200, 700), and [700, 3000). Count products and average price in each.
Covered Query Verification
Execute a covered query on users finding by email, projecting only email and suppressing _id ({ _id: 0, email: 1 }).
Customer Average Rating Analysis
Group products by category, projecting category name, totalStock ($sum: stock), and rounding or averaging rating.
Complex Multi-Stage Revenue Pipeline
Filter orders where total >= 200, group by category to find revenue and item count, and sort by revenue descending.
Tag Popularity Unwind and Filter
Unwind user tags, group by tag to count occurrences, and filter for tags shared by 2 or more users ($match: count >= 2).
End-to-End Infrastructure Audit
Filter servers where status = "active", group by region to compute nodeCount, totalCores, and average loadAvg (avgLoad), sorted by nodeCount descending.