I have the following test that passes:
namespace MongoDateTest
{
[TestFixture]
public class DateTesting
{
public class TestEntity
In most case you want to store UTC date times in the database so your DateTime should be constructed as:-
DateTest = new DateTime(2013, 10, 13, 0, 0, 0, DateTimeKind.Utc) //this is the date
With this the first of your commented unit tests now passes.
Without specifying the DateTimeKind
you are leaving it up to chance. MongoDB appears to assume that it's local and converts it to UTC in the database.
Note also that MongoDB DateTime values have less precision than .NET DateTime values. If you want to store arbitrary DateTime values and get them back in such a way that they still match then you will need to round them to the nearest millisecond before storing them.
If you really do want to store local times I recommend you switch from DateTime
to DateTimeOffset
and serialize it as a long Tick value for the UTC DateTime and a value for the offset.
Note that unless you store the offset calculated at the time the DateTime value was obtained then the .NET methods to convert to LocalTime are essentially useless since they do not know when daylight savings time started, nor do they even know what zone the DateTime value comes from. Overall, .NET DateTime handling leaves a lot to be desired and contains many misleading methods that claim to help but really do not.
You could also do this in your model. public class TestEntity
{
public string Id { get; set; }
public string StringTest { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime DateTest { get; set; }
}