How do I extract the created date out of a Mongo ObjectID
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MongoDB is a popular NoSQL database known for its scalability and flexibility. One of the unique features of MongoDB is the way it uses ObjectIDs as a primary key. These ObjectIDs contain built-in information, such as the timestamp indicating when the document was created. This article will guide you through the process of extracting the creation date from a MongoDB ObjectID, offering technical insights along the way.
Understanding MongoDB ObjectID
A MongoDB ObjectID is a 12-byte identifier for each document in a MongoDB collection. It is unique across the entire database and consists of the following components:
- Timestamp (4 bytes): The first 4 bytes represent an epoch timestamp, meaning they provide the date and time when the ObjectID was created.
- Machine Identifier (3 bytes): A unique identifier of the machine on which the MongoDB server is running.
- Process ID (2 bytes): The process identifier of the MongoDB server instance.
- Counter (3 bytes): An incrementing counter, initialized to a random value.
Given the first 4 bytes encode the Unix epoch time in seconds, you can extract this timestamp to determine when the document was created.
Extracting the Created Date
To extract the created date from an ObjectID, you can manipulate the ID using programming languages or MongoDB's native capabilities.
Using MongoDB Shell
In the MongoDB shell, you can extract the timestamp directly from an ObjectID using the getTimestamp() method.
Using Python with PyMongo
PyMongo is the official MongoDB driver for Python. You can use it to interact with MongoDB and extract ObjectID timestamps.
Using Node.js with Mongoose
Mongoose is a popular ODM (Object Data Modeling) library for MongoDB and Node.js. It provides methods to work with ObjectIDs.
Key Points
Here's a summary of key points when extracting the created date from a MongoDB ObjectID:
| Component | Bytes | Description |
| Timestamp | 4 | Stores Unix epoch timestamp of ObjectID creation (seconds) |
| Machine ID | 3 | Unique identifier of the MongoDB server's host machine |
| Process ID | 2 | Identifier for the MongoDB server process |
| Counter | 3 | Incrementing counter to ensure uniqueness |
Additional Details
Why Use ObjectID?
MongoDB’s use of ObjectID offers several benefits:
- Uniqueness: Ensures each document has a unique identifier.
- Efficiency: The use of an incrementing counter and machine ID reduces the likelihood of collision.
- Timestamp: Embedded timestamp aids in data analysis and auditing.
Potential Limitations
While extracting the ObjectID timestamp is useful, be aware of potential limitations:
- Resolution: The timestamp is stored in seconds, not milliseconds, meaning that the granularity is limited.
- Timezones: The extracted datetime will be in UTC. Convert it to your local timezone as needed.
Conclusion
Extracting the created date from a MongoDB ObjectID can provide valuable insights into when a document was created, ensuring that you maximize the capabilities of MongoDB's unique identifiers. Whether it's using MongoDB's native tools or leveraging programming languages like Python and Node.js, understanding how to decode an ObjectID will enhance your data management skills in MongoDB.
Related reading
- How do I filter query objects by date range in Django?
- How do I find out my MySQL URL, host, port and username?
- How do I find out my MySQL URL, host, port and username?
- How do I find the MySQL my.cnf location
- How do I find which transaction is causing a Waiting for table metadata lock state?
- How do I get a list of column names from a psycopg2 cursor?
- How do I get a list of tables, the schema, a dump, using the sqlite3 API?
- How do I get a raw, compiled SQL query from a SQLAlchemy expression?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.