0
0
MongoDBquery~30 mins

Date and timestamp types in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Review the documents to confirm all fields and types are correct.