Bird
Raised Fist0
MongoDBquery~20 mins

Date and timestamp types in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Date and Timestamp Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with dates after 2023-01-01

Given a collection events with a date field of type Date, which query returns all documents where the date is after January 1, 2023?

Adb.events.find({ date: { $gt: new Date('2023-01-01') } })
Bdb.events.find({ date: { $gte: '2023-01-01' } })
Cdb.events.find({ date: { $gt: ISODate('2023-01-01') } })
Ddb.events.find({ date: { $gt: '2023-01-01T00:00:00Z' } })
Attempts:
2 left
💡 Hint

Use the $gt operator with a Date object to compare dates.

🧠 Conceptual
intermediate
2:00remaining
Difference between Date and Timestamp types

Which statement correctly describes the difference between MongoDB's Date and Timestamp types?

A<code>Date</code> is deprecated; <code>Timestamp</code> replaces it for all date storage.
B<code>Date</code> stores only date; <code>Timestamp</code> stores date and time.
C<code>Date</code> stores a date and time; <code>Timestamp</code> is for internal replication and stores a time and increment.
D<code>Date</code> stores time in seconds; <code>Timestamp</code> stores time in milliseconds.
Attempts:
2 left
💡 Hint

Think about how MongoDB uses Timestamp internally for replication.

📝 Syntax
advanced
2:00remaining
Correct syntax to create a Timestamp

Which option correctly creates a MongoDB Timestamp with time 1672531200 and increment 1?

ATimestamp({time: 1672531200, inc: 1})
BTimestamp(1672531200, 1)
Cnew Timestamp({t: 1672531200, i: 1})
Dnew Timestamp(1672531200, 1)
Attempts:
2 left
💡 Hint

Use the new keyword with two numeric arguments for time and increment.

optimization
advanced
2:00remaining
Efficiently query documents by date range

You want to find all documents in logs collection with createdAt between 2023-01-01 and 2023-01-31. Which query is most efficient assuming createdAt is indexed?

Adb.logs.find({ createdAt: { $gte: ISODate('2023-01-01'), $lte: ISODate('2023-01-31') } })
Bdb.logs.find({ createdAt: { $gte: new Date('2023-01-01'), $lt: new Date('2023-02-01') } })
Cdb.logs.find({ createdAt: { $gt: new Date('2023-01-01'), $lt: new Date('2023-01-31') } })
Ddb.logs.find({ createdAt: { $gte: '2023-01-01', $lt: '2023-02-01' } })
Attempts:
2 left
💡 Hint

Use inclusive start and exclusive end dates for ranges to avoid overlap.

🔧 Debug
expert
2:00remaining
Why does this date query return no results?

Given documents with eventDate stored as Date type, why does this query return no results?

db.events.find({ eventDate: { $eq: '2023-03-15T00:00:00Z' } })
ABecause the query compares a string to a Date type, causing no matches.
BBecause the date format string is invalid and causes a syntax error.
CBecause the field <code>eventDate</code> does not exist in documents.
DBecause <code>$eq</code> cannot be used with dates in MongoDB.
Attempts:
2 left
💡 Hint

Check the data types used in the query and stored in the documents.

Practice

(1/5)
1. Which MongoDB data type is best for storing a human-readable date and time?
easy
A. Number
B. ISODate
C. String
D. Timestamp

Solution

  1. Step 1: Understand MongoDB date types

    ISODate stores dates in a readable and standard format, suitable for queries and sorting.
  2. Step 2: Compare with other types

    Timestamp is mainly for internal use, String and Number do not store dates natively.
  3. Final Answer:

    ISODate -> Option B
  4. Quick Check:

    Date type for readable time = ISODate [OK]
Hint: Use ISODate for readable and queryable dates [OK]
Common Mistakes:
  • Confusing Timestamp with ISODate
  • Using String to store dates
  • Using Number for date storage
2. Which of the following is the correct way to create a date object for January 1, 2023 in MongoDB shell?
easy
A. Timestamp("2023-01-01")
B. new Date(2023-01-01)
C. ISODate("2023-01-01")
D. Date("2023-01-01")

Solution

  1. Step 1: Recall MongoDB date creation syntax

    ISODate() is the correct function to create a date object in MongoDB shell.
  2. Step 2: Check other options

    new Date() is JavaScript syntax but not recommended in MongoDB shell; Timestamp() is for internal timestamps; Date() returns string, not date object.
  3. Final Answer:

    ISODate("2023-01-01") -> Option C
  4. Quick Check:

    MongoDB date object = ISODate() [OK]
Hint: Use ISODate() to create dates in MongoDB shell [OK]
Common Mistakes:
  • Using new Date() instead of ISODate()
  • Using Timestamp() for normal dates
  • Calling Date() without new keyword
3. Given the following MongoDB document:
{ "event": "meeting", "time": ISODate("2024-06-15T10:00:00Z") }

What will be the result of this query?
db.events.find({ time: { $gt: ISODate("2024-06-01T00:00:00Z") } })
medium
A. Returns documents with time after June 1, 2024
B. Returns documents with time before June 1, 2024
C. Returns all documents regardless of time
D. Syntax error in query

Solution

  1. Step 1: Understand the query filter

    The query uses $gt (greater than) to find documents where time is after June 1, 2024.
  2. Step 2: Check document time value

    The document time is June 15, 2024, which is after June 1, 2024, so it matches the filter.
  3. Final Answer:

    Returns documents with time after June 1, 2024 -> Option A
  4. Quick Check:

    $gt filter returns later dates [OK]
Hint: Use $gt to find dates after a given date [OK]
Common Mistakes:
  • Confusing $gt with $lt
  • Expecting syntax error
  • Thinking it returns all documents
4. What is wrong with this MongoDB query to find documents with a timestamp field after a certain date?
db.logs.find({ timestamp: { $gt: "2024-01-01T00:00:00Z" } })
medium
A. The $gt operator is invalid
B. The query syntax is correct
C. The field name 'timestamp' is reserved
D. The date string should be wrapped in ISODate()

Solution

  1. Step 1: Check the query filter value type

    The query compares the timestamp field to a string, but MongoDB expects a Date object for date comparisons.
  2. Step 2: Correct the date format

    Wrapping the date string in ISODate() converts it to a Date object, making the query valid.
  3. Final Answer:

    The date string should be wrapped in ISODate() -> Option D
  4. Quick Check:

    Date comparisons need ISODate() objects [OK]
Hint: Wrap date strings with ISODate() for date queries [OK]
Common Mistakes:
  • Using plain strings for date comparison
  • Assuming $gt is invalid
  • Thinking field names are reserved
5. You want to store user login times and also track the exact order of logins including multiple logins in the same second. Which MongoDB data type combination should you use?
hard
A. Use ISODate for login time and Timestamp for order tracking
B. Use only ISODate for both time and order
C. Use Timestamp for login time and String for order
D. Use String for both login time and order

Solution

  1. Step 1: Understand the use of ISODate and Timestamp

    ISODate stores readable date/time; Timestamp is precise and used internally to track order of operations.
  2. Step 2: Apply to login tracking

    Use ISODate to record when login happened, and Timestamp to track exact order if multiple logins occur at the same second.
  3. Final Answer:

    Use ISODate for login time and Timestamp for order tracking -> Option A
  4. Quick Check:

    ISODate for time + Timestamp for order = correct [OK]
Hint: Combine ISODate and Timestamp for time and order tracking [OK]
Common Mistakes:
  • Using only ISODate loses order precision
  • Using Timestamp for readable time
  • Using strings instead of date types