Explorer
MongoDB

Add a `tier` field to orders: orders with total >= 500 get "premium", >= 100 get "standard", else "basic". Then count orders per tier.

Problem Statement

<p>Add a <code>tier</code> field to <code>orders</code>: <code>orders</code> with total >= 500 get "premium", >= 100 get "standard", else "basic". Then count <code>orders</code> per tier.</p>

Examples

Input: orders collection: +-----+-------+ | _id | total | +-----+-------+ | 1 | 50 | | 2 | 150 | | 3 | 750 | | 4 | 200 | | 5 | 30 | | 6 | 500 | +-----+-------+

Output: +----------+-------+ | tier | count | +----------+-------+ | premium | 2 | | standard | 2 | | basic | 2 | +----------+-------+

Explanation: The collection documents are sorted according to the specified field order.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Use comparison operators ($gt, $gte, $lt, $lte) to filter documents based on numeric or date ranges. šŸ’” Hint 2: Construct the filter condition: { fieldName: { $gt: value } } inside db.<collection>.find(...). šŸ’” Hint 3: Chain projection or sorting if requested, and invoke .toArray() to produce the result array.

Editorial & Approach

Problem Overview & Intuition

To solve "E-commerce: Order Value Distribution", we query the MongoDB document store. The goal is to add a `tier` field to orders: orders with total >= 500 get "premium", >= 100 get "standard", else "basic". then count orders per tier. Using a targeted find query, the database engine filters and structures the BSON documents efficiently.

Step-by-Step Approach

  1. Identify Target Collection: Access the collection through the db instance.
  2. Construct Query / Pipeline: Build the query filter with appropriate comparison operators.
  3. Resolve Cursor: Invoke .toArray() to transform the query cursor into the required array of documents.

Optimal Implementation (MongoDB)

function solve(db) {
  const premium = db.orders.countDocuments({ total: { $gte: 500 } });
  const standard = db.orders.find({ $and: [{ total: { $gte: 100 } }, { total: { $lt: 500 } }] }).toArray().length;
  const basic = db.orders.countDocuments({ total: { $lt: 100 } });
  return [{ tier: "premium", count: premium }, { tier: "standard", count: standard }, { tier: "basic", count: basic }];
}

Complexity Analysis

Time Complexity O(N) collection scan (O(log N) if index is present on filtered fields).
Space Complexity O(K) where K is the number of returned documents in memory.

Key Considerations & Edge Cases

  • Empty Collections: If no documents match, the query cleanly returns an empty array [].
  • Missing / NULL Fields: Missing fields in documents are handled safely without throwing runtime exceptions.
  • Type Coercion: BSON types (ObjectId, Numbers, Strings) are compared strictly according to MongoDB specifications.

E-commerce: Order Value Distribution

Hard

Add a tier field to orders: orders with total >= 500 get "premium", >= 100 get "standard", else "basic". Then count orders per tier.

Example Scenarios
1Example 1
Input:
orders collection
_idtotal
150
2150
3750
4200
530
6500
Output:
tiercount
premium2
standard2
basic2
Explanation:

The collection documents are sorted according to the specified field order.

MongoDB Editor
Loading Editor...
Query Results (JSON)

Run your MongoDB code to see results here.