BsonDocument DateTime discrepancy?

298 views
Skip to first unread message

Stane

unread,
Aug 26, 2012, 4:37:04 AM8/26/12
to mongodb...@googlegroups.com
Hi,
I am rather new in MongoDB, now trying to implement it in my next project. A few silly questions are unavoidable, so I will be glad if someone who happens to know the solution would descend to answer.

Concretely, i am serializing a structure containing a C# DateTime field, e.g. new DateTime(2010, 5, 12) which is 2010-05-12 00:00:00.
After storing on a Mongo collection and retrieving the item I obtain the DateTime value sometimes as 2010-05-11 23:00:00, sometimes as 2010-05-11 22:00:00. This happens even if I created the datetime with exact hour, munute, and second setting them to 0.

Does anybody know this phenomenon and how can I get correct dateTime values?

TIA,
Stane

Daniel Harman

unread,
Aug 26, 2012, 5:35:35 AM8/26/12
to mongodb...@googlegroups.com
Sounds odd to me. Can't see how it would impact really, but are you storing a UTC date time as this moving by hours feels like a timeline issue. Probably teaching you to suck eggs, but have a look at he '.Kind' property before and after serialisation.

One thing you might want to consider as it works in a lot of situations, is that objectid contains a date time to 1 second resolution. This has saved me having to store date time in a lot of circumstances as I already have objectid as the key to all my tables.

One other option if you only want date is to just store those values. E.g. Make your own struct. Date time is not a very compact way of storing time stamps. Even more compact would be days since an epoch which is similar to unix dates. (.net stores 16 bit per segment of date time iirc)

Dan

Sent from my iPad

Stane

unread,
Aug 26, 2012, 6:45:33 AM8/26/12
to mongodb...@googlegroups.com
Sounds odd to me, too. As a newcomer I have naturally awaited smooth functioning. I have already tried to vary some of the attributes, but not yet all combinations.

>> that objectid contains a date time to 1 second resolution
would be more than precise for my purposes


>> to just store those values. E.g. Make your own struct.
Sorry, this I would find totally uncivilized. What is then DataTime there for?

Well, I must try all combinations of BsonDateTimeOptions and BsonRepresentation...

BTW, I am using Version 1.2, mongo Version 2.0, .NET 3.5, VS 2010 (Windows 7).

Thanks, Stan

Stane

unread,
Aug 26, 2012, 7:33:39 AM8/26/12
to mongodb...@googlegroups.com
Additional example:
DateTime dt0                = new DateTime(2010, 11, 25);
BsonDocument doc0    = new BsonDocument().Add("_id", "aaa").Add("DateTime", dt0);
// doc0    { "_id" : "aaa", "DateTime" : ISODate("2010-11-24T23:00:00Z")}

Can someone please explain me why this happens and how to store DateTime correctly on a BsonDocument?
TIA, Stan

Robert Stam

unread,
Aug 26, 2012, 8:38:12 AM8/26/12
to mongodb...@googlegroups.com
The BSON spec requires that DateTime values be in UTC. The C# driver converts DateTime values to UTC if they are not already in UTC.

Daniel Harman

unread,
Aug 26, 2012, 8:59:08 AM8/26/12
to mongodb...@googlegroups.com

You need the 4th param on the constructor and set it to Utc.

And when you grab current timestamp get UtcNow.

As to creating a struct, it depends if you want to optimise for space and use case. The framework structure is not very memory efficient but obviously much easier to use something already written. I mentioned it as the timestamp in objectid is based on the unix design which is seconds since epoch rather than a multi-segment representation.

Dan

Stane

unread,
Aug 26, 2012, 11:06:56 AM8/26/12
to mongodb...@googlegroups.com
Thanks Robert, thanks Daniel,
thanks for the fruitful tip. I never knew that the DateTime(int, int, int) constructor creates local time! I am afraid, there is no mention of the fact in the official documentation. Interesting that I never stumbled upon this peculiarity since I have been working with C# (5 years at least).
Anyway, this works.
The constructor to use needs, however, not 4, but 7 (!) parameters, of which Kind is the last. But this makes sense.

Stan
Reply all
Reply to author
Forward
0 new messages