C# .NET- GetTimeStamp in MongoDB using ObjectId

C# .NET- GetTimeStamp in MongoDB ID ObjectId

Today in this article, we will see how to get the time stamp using C# .NET- GetTimeStamp in MongoDB ID ObjectId.

Updated October 2026: a CreationTime vs Timestamp explanation, a date-range query section, ordering caveats and an FAQ.

Timestamp details could be useful for many other reasons and hence we will see how to get this timestamp.

ObjectId is the 12-byte element consisting of a timestamp value, a random value, and an incremental counter.

MongoDB _id field as ObjectId

MongoDB uses the _id field as ObjectId, this Id has few behavioral characteristics and it is explained below,

  • MongoDB database automatically generates _id the field for insertion.
  • This field is unique for every document inserted into the collection.
  • _id field is automatically indexed and the index is unique.

A 4-byte timestamp is a UNIX timestamp. It represents the ObjectId’s creation timestamp, measured in seconds since the Unix epoch.

Getting started

Please create any C# .NET/.NET Core application.

Please add the MongoDB driver NuGet package using the Nuget Package manager,

PM> Install-Package MongoDB.Driver -Version 2.9.3


Note: Please use the latest driver available

Step I – Establish the connection to the Database using MongoDB driver,

var _mongoClient = new MongoClient("mongodb://your connection string");
var db = _mongoClient.GetDatabase("your database name");

If using ASP.NET Core, please use dependency injection for injecting the driver instance,

GetTimeStamp in MongoDB using ObjectId

     var filter       =  Builders<BsonDocument>.Filter.Eq("_id", 
                         (ObjectId)rootObject.GetElement("_id").Value);

     ObjectId objMongoId = (ObjectId)rootObject.GetElement("_id").Value;
               
     var creationDate = objMongoId .CreationTime;

ObjectId gives access to CreationTime, Increment Timestamp properties,

C# .NET- GetTimeStamp MongoDB ID ObjectId, MongoDB Get time using ID

Please note that this timestamp is the timestamp of the document( for that id) when it was inserted the very first time in mongo collection.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.



CreationTime vs Timestamp: which ObjectId property should you use?

Use CreationTime when you want a date. Use Timestamp only when you need the raw seconds value, usually to build or compare boundary ObjectIds.

These are the members you’ll see on the ObjectId struct in MongoDB.Bson:

  • CreationTime: a DateTime derived from the 4-byte timestamp. It is UTC, so call ToLocalTime() only when you display it.
  • Timestamp: the raw integer of seconds since the Unix epoch. It isn’t a date.
  • Increment, Machine and Pid: low-level or legacy members. You rarely need them for reading a creation date.

Why two documents can share the same CreationTime

The resolution is one second. Two documents created in the same second have the same CreationTime, so don’t treat it as a precise insertion order.

The value also comes from the application’s clock. When _id is empty, the driver generates the ObjectId on the client, and the MongoDB server never sets the timestamp.

In plain English: It’s like a parcel label printed at the sender’s house. The date on it comes from the sender’s clock, not the post office’s. If your app server’s clock is wrong, every label it prints is wrong too.

Is the old machine ID and PID layout still valid?

No. Since the layout change between MongoDB 3.2 and 3.4, the middle 5 bytes are a random value, not a machine ID plus process ID. Older articles that describe the machine and PID are out of date. The timestamp bytes didn’t change, so CreationTime works the same either way.

What is the C# equivalent of getTimestamp()?

In mongosh, ObjectId.getTimestamp() returns a Date. That is a shell method, not a driver API. In C#, CreationTime is the equivalent.

Updating a document never changes its ObjectId. The value always reflects the first insert, not the last modification.

Frequently asked questions

How do I get the creation date from a MongoDB ObjectId in C#?

Read the CreationTime property on the MongoDB.Bson ObjectId struct. It returns a UTC DateTime with one-second precision. You can get an ObjectId from a typed Id property on your model, or by reading _id from a BsonDocument with AsObjectId.

What is the difference between ObjectId.CreationTime and ObjectId.Timestamp in the .NET driver?

CreationTime is a DateTime derived from the timestamp. Timestamp is the raw integer count of seconds since the Unix epoch. Use CreationTime for dates, and reach for Timestamp only when you need the raw seconds value, for example to build a boundary ObjectId for a range query.

Is the ObjectId timestamp in UTC or local time?

It is UTC, counted in seconds since the Unix epoch. In C# the driver generates the ObjectId on the client, so the timestamp comes from your application’s clock, not the server’s. A badly set clock on the machine gives a wrong date. Convert to local time only when you display the value.

Can I rely on ObjectId timestamps instead of a createdAt field?

Only for a rough, second-level creation time on documents whose ObjectIds were generated by the driver. Ordering is approximate, because clients can have clock skew and several IDs can be created in the same second. Store an explicit createdAt field when you need millisecond precision, when you import data, when you use custom _id values, or when the date is business-critical.

Leave a Reply

Your email address will not be published. Required fields are marked *