Can I query MongoDB ObjectId by date?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
MongoDB's ObjectId is a 12-byte identifier that serves as the default primary key for unique identification of documents in a collection. The structure of an ObjectId encodes a timestamp representing the time it was generated, which can be very useful for querying by date. This article explores how you can query MongoDB documents using the date encoded in ObjectId.
Understanding MongoDB ObjectId
An ObjectId is 12 bytes long, and its structure is as follows:
- 4 bytes: Timestamp - A Unix epoch timestamp in seconds.
- 3 bytes: Machine Identifier - A unique identifier for the machine.
- 2 bytes: Process Identifier - A unique identifier for the process.
- 3 bytes: Counter - An incrementing counter, initialized to a random value.
Here's a visual breakdown:
| Byte Offset | Section Name | Size (Bytes) | Description |
| 0-3 | Timestamp | 4 | Unix timestamp in seconds |
| 4-6 | Machine Identifier | 3 | Machine ID |
| 7-8 | Process Identifier | 2 | Process ID |
| 9-11 | Counter | 3 | Incrementing integer |
Extracting the Timestamp from ObjectId
The timestamp from an ObjectId can be extracted in several programming languages. Below is an example using Node.js and the native MongoDB driver:
The getTimestamp method converts the 4-byte timestamp into a JavaScript Date object.
Querying MongoDB by ObjectId Date
To query documents by date, you will need to compute the start and end ObjectIds that represent your desired time range. The timestamp should be converted back into an ObjectId by appending the rest of the bytes as zeroes or any arbitrary value.
Example Scenario
Let's retrieve all documents created on a specific date (e.g., 2022-01-01):
- Calculate the start of the date time (e.g.,
2022-01-01T00:00:00Z). - Calculate the end of the date time (e.g.,
2022-01-01T23:59:59Z).
Convert these timestamps into ObjectId ranges:
Then, query the collection with these bounds:
Key Considerations
- Time Zone Awareness: MongoDB's timestamps are stored in UTC. Take time zone differences into account when querying.
- Performance: Indexes on
_idshould make range queries efficient, independent of date.
Table of Key Points
| Key Aspect | Details |
ObjectId Structure | 12 bytes: timestamp, machine ID, process ID, counter |
| Timestamp Extraction | getTimestamp method in multiple languages |
| Date Query Strategy | Calculate range, construct ObjectIds, filter |
| Time Zone Awareness | Consider UTC offset for accurate results |
| Performance | Utilize indexed _id field for efficient queries |
Additional Topics
Why Use ObjectId for Date Queries?
- Efficiency: Utilizes indexed field for quick retrieval.
- Simplicity:
ObjectIdautomatically encodes creation time, reducing storage overhead.
Alternatives to ObjectId for Date Queries
- Use an explicit date field with the
$ltand $gtoperators. - Leverage compound indexes on date fields for complex queries.
By understanding and utilizing the timestamp within the ObjectId, developers can execute time-based queries efficiently, leveraging MongoDB's document storage capabilities. This approach is ideal when the date queries are simple and revolve around the creation time of documents.

