0
0
MongoDBquery~20 mins

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

Choose your learning style9 modes available
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.