[CSHARP-1483] Serialization of DateTimeOffset fields Created: 20/Nov/15  Updated: 15/Nov/21  Resolved: 04/Dec/15

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.0.2
Fix Version/s: None

Type: Task Priority: Critical - P2
Reporter: Hendrik Rümmler Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hi,
in our object model we have some DateTimeOffset properties that are stored in mongodb like that when using the c# driver.

"ct" : [NumberLong("635832589587284402"), 0]

Now we wanted to put a compoundindex on our document that included one of those DateTimeOffset fields. We found out that the index would make our query not work anymore when it includes the DateTimeOffset field. So far so good. The DateTimeOffset is stored as array of Ticks in LocalTime and the offset to UTC.

We could make the index work by using something like ct.0 but with that wewould only query on the ticks in localTime. For us this might lead to inconsistent results so we don't want to go that way.

What is the best approach to solve this issue? We are thinking about our own serializer for the DateTimeOffset fields so we can serlialze those fields in UTC.

Regards
Hendrik



 Comments   
Comment by Craig Wilson [ 20/Nov/15 ]

Nope... If we've done LINQ right, it should just work unless you want to search by certain sub properties of the DateTimeOffset. If that is the case, you'll need to implement IBsonDocumentSerializer or IBsonArraySerializer with respect to your chosen serialiization format.

Comment by Hendrik Rümmler [ 20/Nov/15 ]

Thanks for your response. How would this interact with LINQ? The serializer works for the store and restore of the property but how does a changed serialization work togehter with LINQ to MongoDB query. Is there something else we need to implement?

Comment by Craig Wilson [ 20/Nov/15 ]

If you need something different than the current serialization format, you'll need to write your own serializer. The existing serializer is here: https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Bson/Serialization/Serializers/DateTimeOffsetSerializer.cs.

Generated at Wed Feb 07 21:39:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.