Bird
Raised Fist0
MongoDBquery~20 mins

Time-series collections in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Time-series Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Querying time-series data with a time filter
Given a MongoDB time-series collection named sensorData with documents containing timestamp and temperature fields, what will be the output of this query?

db.sensorData.find({ timestamp: { $gte: ISODate('2024-01-01T00:00:00Z'), $lt: ISODate('2024-01-02T00:00:00Z') } })

Assume the collection has 3 documents with timestamps on 2023-12-31, 2024-01-01, and 2024-01-02.
MongoDB
db.sensorData.find({ timestamp: { $gte: ISODate('2024-01-01T00:00:00Z'), $lt: ISODate('2024-01-02T00:00:00Z') } })
AReturns no documents
BReturns documents with timestamps on 2024-01-01 and 2024-01-02
CReturns only the document with timestamp on 2024-01-01
DReturns all three documents
Attempts:
2 left
💡 Hint
Think about the meaning of $gte and $lt in the time range filter.
🧠 Conceptual
intermediate
2:00remaining
Understanding time-series collection schema requirements
Which of the following is required when creating a time-series collection in MongoDB?
MongoDB
db.createCollection('sensorData', { timeseries: { timeField: 'timestamp', metaField: 'deviceId' } })
AThe <code>timeField</code> must be specified and contain date values
BThe <code>metaField</code> must be specified and contain numeric values
CThe collection must have an index on the <code>timeField</code> before creation
DThe collection must be capped
Attempts:
2 left
💡 Hint
Think about what MongoDB uses to organize time-series data.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in time-series collection creation
Which option contains a syntax error when creating a time-series collection named weatherData with recordedAt as the time field?
Adb.createCollection('weatherData', { timeseries: { timeField: 'recordedAt', metaField: 123 } })
Bdb.createCollection('weatherData', { timeseries: { timeField: 'recordedAt' } })
Cdb.createCollection('weatherData', { timeseries: { timeField: 'recordedAt', metaField: 'location' } })
Ddb.createCollection('weatherData', { timeseries: { timeField: 'recordedAt', granularity: 'minutes' } })
Attempts:
2 left
💡 Hint
Check the type of the metaField value.
❓ optimization
advanced
2:00remaining
Optimizing queries on time-series collections
You want to optimize queries filtering by device ID and time range on a time-series collection sensorReadings. Which index strategy is best?
ANo indexes needed because time-series collections are always optimized
BCreate a compound index on <code>{ deviceId: 1, timestamp: 1 }</code>
CCreate a single index on <code>timestamp</code> only
DCreate a single index on <code>deviceId</code> only
Attempts:
2 left
💡 Hint
Think about filtering by both device and time.
🔧 Debug
expert
2:00remaining
Why does this time-series query return no results?
You run this query on a time-series collection tempLogs:

db.tempLogs.find({ timestamp: { $gt: new Date('2024-01-01T00:00:00Z') } })

But it returns no documents, even though you know data exists after that date. What is the most likely reason?
MongoDB
db.tempLogs.find({ timestamp: { $gt: new Date('2024-01-01T00:00:00Z') } })
AThe collection is empty
BThe query syntax is invalid and causes an error
CThe dates in the collection are stored as strings, not dates
DThe <code>timestamp</code> field is not the timeField defined in the collection
Attempts:
2 left
💡 Hint
Check the collection's timeField configuration.

Practice

(1/5)
1. What is the main purpose of a time-series collection in MongoDB?
easy
A. To manage user authentication and roles
B. To store large binary files like images and videos
C. To efficiently store and query data that changes over time
D. To create backups of the database automatically

Solution

  1. Step 1: Understand time-series data

    Time-series data is data recorded over time, like temperature or stock prices.
  2. Step 2: Identify MongoDB time-series collection use

    MongoDB time-series collections optimize storage and queries for such time-based data.
  3. Final Answer:

    To efficiently store and query data that changes over time -> Option C
  4. Quick Check:

    Time-series collections = efficient time-based data storage [OK]
Hint: Time-series collections are for data changing over time [OK]
Common Mistakes:
  • Confusing time-series collections with file storage
  • Thinking time-series collections manage users
  • Assuming they handle backups automatically
2. Which of the following is required when creating a time-series collection in MongoDB?
easy
A. A field that stores binary data
B. A unique index on the _id field
C. A field that stores user roles
D. A field that stores the timestamp of each record

Solution

  1. Step 1: Recall time-series collection requirements

    MongoDB requires a time field to track when each data point was recorded.
  2. Step 2: Check other options

    Unique _id index exists by default but is not specific to time-series; user roles and binary data fields are unrelated.
  3. Final Answer:

    A field that stores the timestamp of each record -> Option D
  4. Quick Check:

    Time field required = A field that stores the timestamp of each record [OK]
Hint: Time field is mandatory for time-series collections [OK]
Common Mistakes:
  • Choosing unrelated fields like user roles
  • Confusing default indexes with required time field
  • Thinking binary data field is required
3. Given the following command, what will be the result?
db.createCollection("sensorData", {
  timeseries: {
    timeField: "timestamp",
    metaField: "deviceId"
  }
})
medium
A. Creates a time-series collection with timestamp and deviceId fields
B. Throws a syntax error due to missing options
C. Creates a regular collection named sensorData
D. Creates a capped collection instead of time-series

Solution

  1. Step 1: Analyze the createCollection command

    The command uses the timeseries option with timeField and metaField specified.
  2. Step 2: Understand MongoDB behavior

    This creates a time-series collection named sensorData with timestamp as time and deviceId as metadata.
  3. Final Answer:

    Creates a time-series collection with timestamp and deviceId fields -> Option A
  4. Quick Check:

    timeseries option creates time-series collection [OK]
Hint: timeseries option creates time-series collection [OK]
Common Mistakes:
  • Thinking it creates a regular or capped collection
  • Assuming syntax error due to missing options
  • Confusing metaField with required timeField
4. You tried to create a time-series collection but got an error. Which of the following is the most likely cause?
db.createCollection("temps", {
  timeseries: {
    metaField: "location"
  }
})
medium
A. Missing required timeField in timeseries options
B. metaField cannot be used in time-series collections
C. Collection name is invalid
D. timeseries option is misspelled

Solution

  1. Step 1: Check timeseries options in the command

    The command specifies metaField but omits the required timeField.
  2. Step 2: Recall MongoDB requirements

    timeField is mandatory for time-series collections; missing it causes an error.
  3. Final Answer:

    Missing required timeField in timeseries options -> Option A
  4. Quick Check:

    timeField missing = error [OK]
Hint: Always include timeField when creating time-series collections [OK]
Common Mistakes:
  • Thinking metaField is not allowed
  • Assuming collection name is invalid
  • Believing timeseries option is misspelled
5. You have a time-series collection with fields timestamp (timeField) and sensorId (metaField). You want to query the average temperature for sensorId 'S1' over the last 24 hours. Which aggregation pipeline is correct?
db.sensorData.aggregate([
  { $match: { sensorId: 'S1', timestamp: { $gte: new Date(Date.now() - 86400000) } } },
  { $group: { _id: null, avgTemp: { $avg: "$temperature" } } }
])
hard
A. Groups by sensorId instead of null
B. Correctly filters by sensorId and timestamp, then averages temperature
C. Uses $sort instead of $match for filtering
D. Filters only by timestamp, ignoring sensorId

Solution

  1. Step 1: Analyze the $match stage

    The pipeline filters documents where sensorId is 'S1' and timestamp is within last 24 hours.
  2. Step 2: Analyze the $group stage

    It groups all matched documents (_id: null) and calculates average temperature.
  3. Final Answer:

    Correctly filters by sensorId and timestamp, then averages temperature -> Option B
  4. Quick Check:

    Filter by sensorId and time, then average = Correctly filters by sensorId and timestamp, then averages temperature [OK]
Hint: Match filters, group aggregates for averages [OK]
Common Mistakes:
  • Forgetting to filter by sensorId
  • Using $sort instead of $match for filtering
  • Grouping by sensorId when only one sensor is queried
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard