Top Active Product Variants
You are managing an e-commerce catalog stored in MongoDB. Each document in the products collection represents a product with nested variants arrays. Each variant has an sku, an active boolean flag, and a pricing subdocument containing a base price.
Collections
products: contains_id,name,category, andvariants(an array of subdocuments).- Variant fields:
sku(string),active(boolean), andpricing(object withbasenumber).
Task
Find all active product variants whose base price is greater than 50. Unwind the variants array, filter by active: true and pricing.base > 50, and return a flat list of documents containing the product name, variant SKU, and base price. Sort the results in descending order by the base price, and then ascending by the variant SKU.
Example
For a product named "Wireless Mouse" with a variant SKU "MOU-01" having active: true and pricing.base: 59.99, the output should include this variant.
| base_price | product_name | variant_sku |
|---|---|---|
| 89.99 | Pro Keyboard | KEY-01 |
| 55 | Ergonomic Mouse | MOU-02 |
Submitting also runs your answer against 1 hidden dataset, each built around an edge case: NULLs, ties, empty tables. A failure names the case without showing its data.
Follow-up: How would you modify the aggregation pipeline to return only the single most expensive active variant per product?
Return a projection with fields `product_name`, `variant_sku`, and `base_price`. Order the output by `base_price` descending, then `variant_sku` ascending. Exclude products or variants that do not meet the criteria.
- Views
- 2