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
Working with Date and Timestamp Types in MongoDB
📖 Scenario: You are managing a small library database. You want to store information about when books were added to the library and when they were last borrowed. This requires using date and timestamp types in MongoDB.
🎯 Goal: Build a MongoDB collection called library that stores book documents with fields for title, author, addedDate (a date type), and lastBorrowedTimestamp (a timestamp type).
📋 What You'll Learn
Create a collection named library with at least two book documents.
Each book document must have a title and author field as strings.
Add an addedDate field using the MongoDB Date type.
Add a lastBorrowedTimestamp field using the MongoDB Timestamp type.
Use exact field names and types as specified.
💡 Why This Matters
🌍 Real World
Libraries, bookstores, and inventory systems often need to track when items were added and last used. Using date and timestamp types helps manage this information accurately.
💼 Career
Understanding how to use date and timestamp types in MongoDB is important for database administrators and developers working with time-sensitive data.
Progress0 / 4 steps
1
Create the library collection with two book documents
Create a MongoDB collection called library and insert two documents. Each document must have the fields title and author with these exact values: first book title is "The Great Gatsby" and author is "F. Scott Fitzgerald"; second book title is "1984" and author is "George Orwell".
MongoDB
Hint
Use db.collection.insertMany() to add multiple documents at once.
2
Add the addedDate field with MongoDB Date type
Update the two documents in the library collection to add an addedDate field. Use the MongoDB Date type with these exact dates: for "The Great Gatsby" use new Date("1925-04-10"), and for "1984" use new Date("1949-06-08").
MongoDB
Hint
Add the addedDate field inside each document using new Date() with the exact date string.
3
Add the lastBorrowedTimestamp field with MongoDB Timestamp type
Modify the two documents in the library collection to add a lastBorrowedTimestamp field. Use the MongoDB Timestamp type with these exact values: for "The Great Gatsby" use Timestamp(1625097600, 1), and for "1984" use Timestamp(1627689600, 1).
MongoDB
Hint
Add the lastBorrowedTimestamp field inside each document using Timestamp() with the exact values.
4
Verify the final library collection structure
Ensure the library collection has two documents with fields title, author, addedDate as a Date type, and lastBorrowedTimestamp as a Timestamp type, with the exact values specified in previous steps.
MongoDB
Hint
Review the documents to confirm all fields and types are correct.
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]