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
Recall & Review
beginner
What is the MongoDB data type used to store dates and times?
MongoDB uses the Date type to store dates and times. It stores the number of milliseconds since the Unix epoch (January 1, 1970).
Click to reveal answer
beginner
How do you create a new Date object in MongoDB shell?
You create a new Date object by calling new Date(). For example, new Date() returns the current date and time.
Click to reveal answer
intermediate
What is the difference between Date and Timestamp types in MongoDB?
Date stores a specific point in time with millisecond precision. Timestamp is mainly used internally for replication and stores a 64-bit value with a seconds part and an increment part.
Click to reveal answer
beginner
How can you query documents with a date field greater than January 1, 2023?
Use a query like { dateField: { $gt: new Date('2023-01-01T00:00:00Z') } } to find documents where dateField is after January 1, 2023.
Click to reveal answer
intermediate
Why is it important to store dates in UTC in MongoDB?
Storing dates in UTC avoids confusion caused by time zones and daylight saving changes. It ensures consistent date and time values across different systems and locations.
Click to reveal answer
Which MongoDB type stores date and time with millisecond precision?
AString
BTimestamp
CDate
DInteger
✗ Incorrect
The Date type stores date and time with millisecond precision in MongoDB.
How do you create a Date object for the current time in MongoDB shell?
ADate.now()
Bnew Date()
CTimestamp()
Dnew Timestamp()
✗ Incorrect
new Date() creates a new Date object with the current date and time.
What is the main use of the Timestamp type in MongoDB?
AStoring time zones
BStoring user birthdates
CStoring string dates
DInternal replication and oplog entries
✗ Incorrect
Timestamp is mainly used internally for replication and oplog entries in MongoDB.
Which query finds documents with a date after January 1, 2023?
A{ dateField: { $gt: new Date('2023-01-01T00:00:00Z') } }
B{ dateField: { $eq: '2023-01-01' } }
C{ dateField: new Date('2023-01-01') }
D{ dateField: { $lt: new Date('2023-01-01') } }
✗ Incorrect
Use $gt with a Date object to find dates greater than January 1, 2023.
Why should dates be stored in UTC in MongoDB?
ATo avoid time zone confusion
BTo speed up queries
CTo save storage space
DTo support string formatting
✗ Incorrect
Storing dates in UTC avoids confusion caused by time zones and daylight saving changes.
Explain the difference between MongoDB's Date and Timestamp types and when to use each.
Think about precision and internal MongoDB operations.
You got /4 concepts.
Describe how to query documents by date in MongoDB and why storing dates in UTC is important.
Consider query syntax and time zone handling.
You got /4 concepts.
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
Step 1: Understand MongoDB date types
ISODate stores dates in a readable and standard format, suitable for queries and sorting.
Step 2: Compare with other types
Timestamp is mainly for internal use, String and Number do not store dates natively.
Final Answer:
ISODate -> Option B
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
Step 1: Recall MongoDB date creation syntax
ISODate() is the correct function to create a date object in MongoDB shell.
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.
Final Answer:
ISODate("2023-01-01") -> Option C
Quick Check:
MongoDB date object = ISODate() [OK]
Hint: Use ISODate() to create dates in MongoDB shell [OK]
The query compares the timestamp field to a string, but MongoDB expects a Date object for date comparisons.
Step 2: Correct the date format
Wrapping the date string in ISODate() converts it to a Date object, making the query valid.
Final Answer:
The date string should be wrapped in ISODate() -> Option D
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
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.
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.
Final Answer:
Use ISODate for login time and Timestamp for order tracking -> Option A
Quick Check:
ISODate for time + Timestamp for order = correct [OK]
Hint: Combine ISODate and Timestamp for time and order tracking [OK]