Dates and timestamps help you store and work with time information in your database. They let you track when things happen.
Date and timestamp types in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
ISODate("YYYY-MM-DDTHH:mm:ss.sssZ")
Timestamp(seconds, increment)ISODate stores a date and time in a readable format.
Timestamp stores a special internal time used mainly for replication and ordering.
Examples
MongoDB
db.events.insertOne({ event: "party", date: ISODate("2024-06-01T18:00:00Z") })MongoDB
db.logs.insertOne({ action: "login", time: Timestamp(1685600000, 1) })MongoDB
db.records.find({ date: { $gte: ISODate("2024-01-01T00:00:00Z") } })Sample Program
This example adds two users with signup dates. Then it finds users who signed up before June 1, 2024.
MongoDB
db.users.insertMany([
{ name: "Alice", signupDate: ISODate("2024-05-20T10:30:00Z") },
{ name: "Bob", signupDate: ISODate("2024-06-01T15:45:00Z") }
])
db.users.find({ signupDate: { $lt: ISODate("2024-06-01T00:00:00Z") } })Important Notes
ISODate is the most common way to store dates in MongoDB.
Timestamps are mostly used internally and for special cases like replication.
Always store dates in UTC to avoid confusion with time zones.
Summary
Use ISODate to store readable date and time values.
Timestamp is a special type mainly for internal use.
Dates help you filter, sort, and calculate time-related data easily.
Practice
1. Which MongoDB data type is best for storing a human-readable date and time?
easy
Solution
Step 1: Understand MongoDB date types
ISODatestores dates in a readable and standard format, suitable for queries and sorting.Step 2: Compare with other types
Timestampis mainly for internal use,StringandNumberdo not store dates natively.Final Answer:
ISODate -> Option BQuick 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
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 CQuick 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:
What will be the result of this query?
{ "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
Solution
Step 1: Understand the query filter
The query uses $gt (greater than) to find documents where time is after June 1, 2024.Step 2: Check document time value
The document time is June 15, 2024, which is after June 1, 2024, so it matches the filter.Final Answer:
Returns documents with time after June 1, 2024 -> Option AQuick 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
Solution
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.Step 2: Correct the date format
Wrapping the date string inISODate()converts it to a Date object, making the query valid.Final Answer:
The date string should be wrapped in ISODate() -> Option DQuick 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
Solution
Step 1: Understand the use of ISODate and Timestamp
ISODatestores readable date/time;Timestampis precise and used internally to track order of operations.Step 2: Apply to login tracking
UseISODateto record when login happened, andTimestampto track exact order if multiple logins occur at the same second.Final Answer:
Use ISODate for login time and Timestamp for order tracking -> Option AQuick 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
