0
0
MongoDBquery~10 mins

Date and timestamp types in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date and timestamp types
Insert Date or Timestamp
Stored as BSON Date or Timestamp
Query or Update
Returned as ISODate or Timestamp Object
Use in Application or Aggregation
Dates and timestamps are stored in MongoDB as special BSON types and can be inserted, queried, and returned as ISODate or Timestamp objects.
Execution Sample
MongoDB
db.events.insertOne({name: "Meeting", date: new Date("2024-06-01T10:00:00Z")})
db.events.find({date: {$gte: new Date("2024-06-01T00:00:00Z")}})
Insert a document with a Date field and query documents with date greater or equal to a specific date.
Execution Table
StepActionInput/ConditionResult/Output
1Insert document{name: "Meeting", date: new Date("2024-06-01T10:00:00Z")}Document stored with BSON Date type for 'date'
2Query documentsFind documents where date >= new Date("2024-06-01T00:00:00Z")Returns documents with date on or after June 1, 2024
3Return resultDocument with date fieldDate shown as ISODate("2024-06-01T10:00:00Z") in output
4Use timestampInsert with Timestamp(1710000000, 1)Stored as BSON Timestamp type
5Query timestampFind documents with timestamp >= Timestamp(1710000000, 0)Returns matching documents with Timestamp fields
6ExitNo more documents match queryQuery ends
💡 Query ends when no more documents match the date or timestamp condition
Variable Tracker
VariableStartAfter InsertAfter QueryFinal
dateundefinedISODate("2024-06-01T10:00:00Z")ISODate("2024-06-01T10:00:00Z")ISODate("2024-06-01T10:00:00Z")
timestampundefinedTimestamp(1710000000, 1)Timestamp(1710000000, 1)Timestamp(1710000000, 1)
Key Moments - 3 Insights
Why does MongoDB show dates as ISODate instead of a plain string?
MongoDB stores dates as BSON Date type internally, and the shell displays them as ISODate for clarity, as shown in execution_table step 3.
What is the difference between Date and Timestamp types in MongoDB?
Date stores a specific point in time, while Timestamp includes a time and an increment for ordering operations, as seen in execution_table steps 4 and 5.
Can I compare dates and timestamps directly in queries?
No, you must compare dates with dates and timestamps with timestamps separately, as shown in the query conditions in execution_table steps 2 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type is the 'date' field stored as after insertion?
ABSON Date
BString
CTimestamp
DNumber
💡 Hint
Check step 1 in execution_table where the document is stored with BSON Date type.
At which step does the query return documents with date on or after June 1, 2024?
AStep 1
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the query action in step 2 filtering by date condition.
If you insert a document with a Timestamp type, which step shows this action?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Step 4 shows insertion with Timestamp type.
Concept Snapshot
MongoDB stores dates as BSON Date type and timestamps as BSON Timestamp type.
Use new Date() to create dates and Timestamp() for timestamps.
Dates show as ISODate() in shell output.
Query dates and timestamps with $gte, $lt, etc., but compare types correctly.
Timestamps include an increment for operation ordering.
Dates represent specific points in time.
Full Transcript
In MongoDB, dates and timestamps are special data types stored in BSON format. When you insert a document with a date field using new Date(), MongoDB stores it as a BSON Date. Queries can filter documents by comparing these dates. The shell displays dates as ISODate for readability. Timestamps are different; they include a time and an increment and are used for operation ordering. You insert timestamps with the Timestamp() constructor and query them similarly but must keep date and timestamp types separate in queries. This visual trace showed inserting and querying both types step-by-step, tracking how values change and how MongoDB handles these types internally.