Lost
Lost

Reputation: 13565

Representation = BsonType.Int64 does not work properly with Datetime

I am decorating one of my DateTime property in my class with Representation = BsonType.Int64 attribute so that it gets stored in the database with Int64 representation of a date.

When I used to store that property as a normal C# Datetime and did not set the value to anything then it would store DateTime.Min in the database. That is perfect because I was doing reading from databse and doing Query.LT operation like following on it:

Query.LT("MyField", DateTime.Now));

And it used to return all the values fine.

Now that I started storing it as BsonType.Int64 and the equivalent of DateTime.Min in BsonType.Int64 is "0". my Query.LT("MyField", DateTime.Now)); fails on all the dates that are stored with DateTime.Min.

Any idea on how to solve this?

Upvotes: 1

Views: 642

Answers (1)

mnemosyn
mnemosyn

Reputation: 46291

The problem is that, during the query, the MongoDB driver doesn't know that you chose an alternate representation.

Hence, you need to query for an Int64 explicitly:

Query.LT("MyField", DateTime.Now.Ticks));

This will work as expected (tested w/ MongoDB 2.1.1, C# driver 1.4.2.4500)

Upvotes: 1

Related Questions