C# driver: How to extract timestamp from _id value

1,868 views
Skip to first unread message

Merl

unread,
Dec 15, 2010, 2:40:25 PM12/15/10
to mongodb-user
Is there a way through the c# driver to pull the timestamp out of the
_id value and query using it?

Ken Egozi

unread,
Dec 15, 2010, 3:05:54 PM12/15/10
to mongod...@googlegroups.com
Im not sure that I understand your question.
what is the timestamp you are talking about? do you use Date as the Id?
if you could share your data model, and preferably the structure of the query and result you are looking for, it would help greatly.


On Wed, Dec 15, 2010 at 9:40 PM, Merl <merl...@gmail.com> wrote:
Is there a way through the c# driver to pull the timestamp out of the
_id value and query using it?

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
Ken Egozi.
http://www.kenegozi.com/blog
http://www.delver.com
http://www.musicglue.com
http://www.castleproject.org
http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

Merl Corry

unread,
Dec 15, 2010, 3:27:17 PM12/15/10
to mongodb-user
Sorry for the confusion.  I was reading that mongodb's generated _id value has a timestamp embedded in the first 4 bytes of the value generated.  I wanted to use that value to do some maintenance queries against my database.  The documentation says that all of the language drivers should have that implemented, but I am not seeing how.

Tony

On Wed, Dec 15, 2010 at 2:40 PM, Merl <merl...@gmail.com> wrote:
Is there a way through the c# driver to pull the timestamp out of the
_id value and query using it?

Ken Egozi

unread,
Dec 15, 2010, 3:42:36 PM12/15/10
to mongod...@googlegroups.com
assuming your id is of type ObjectId, then you can look at Id.Timestamp
I do not think that you can query on that server side though.

Stuart Johnson

unread,
Dec 15, 2010, 6:08:45 PM12/15/10
to mongod...@googlegroups.com
On 15/12/10 19:40, Merl wrote:
> Is there a way through the c# driver to pull the timestamp out of the
> _id value and query using it?
>


I have a collection of logged events, and make use of the _id time
stamp. Not sure how you read it on the 10gen driver, but to do a search,
I wrote a C# extenstion, to convert the DateTime to an _id. Then use
that in your queries.

private static readonly DateTime Epoch = new DateTime(1970, 1, 1,
0, 0, 0, DateTimeKind.Utc);


public static byte[] ToMonGo_id(this DateTime date)
{
byte[] _id = new byte[12];

byte[] secs = BitConverter.GetBytes((UInt32)new
TimeSpan(date.Ticks - Epoch.Ticks).TotalSeconds);
Array.Reverse(secs);
Array.Copy(secs, _id, 4);
return _id;
}

Robert Stam

unread,
Dec 15, 2010, 8:07:11 PM12/15/10
to mongodb-user
You can extract the Timestamp from an ObjectId using the Timestamp
property. There is also the CreationTime property which returns the
Timestamp converted to a DateTime (accurate to 1 second). Both are in
UTC.

Since the Timestamp is the first 4 bytes of an ObjectId you could use
artificially created ObjectIds to search for documents created during
a certain time range. Keep in mind though that you are trusting that
the ObjectIds were created according to the specifications and that
the clock was accurate on the computer that created the ObjectIds.

There is an ObjectId constructor you can use to artificially create
ObjectIds from a timestamp. For example:

int timestamp; // number of seconds since Epoch
var objectId = new ObjectId(timestamp, 0, 0, 0); // no values for
machine, pid and increment

Here's a sample program that illustrates searching by a range of
ObjectIds:

http://www.pastie.org/1381345

You would never actually insert documents this way. I'm just creating
some backdated bogus ObjectIds so I have something to search against.
Reply all
Reply to author
Forward
0 new messages