0
0
MongoDBquery~5 mins

Date and timestamp types in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
How do you create a Date object for the current time in MongoDB shell?
ADate.now()
Bnew Date()
CTimestamp()
Dnew Timestamp()
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
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') } }
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
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.