Mongodb C#驱动程序和ISODate [英] Mongodb c# driver and ISODate

查看:550
本文介绍了Mongodb C#驱动程序和ISODate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过了以下测试:

namespace MongoDateTest
{

    [TestFixture]
    public class DateTesting
    {
        public class TestEntity
        {
            public string Id { get; set; }
            public string StringTest { get; set; }
            public DateTime DateTest { get; set; }

        }
        [Test]
        public void MongoDateConversion()
        {
            const string connectionString = "mongodb://localhost";
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            var database = server.GetDatabase("test");
            var collection = database.GetCollection<TestEntity>("entities");
            var entity = new TestEntity { 
                   Id = "1", 
                   StringTest = "Test",
                   DateTest = new DateTime(2013, 10, 13) //this is the date
            };
            collection.Save(entity);
            var id = entity.Id;
            var query = Query<TestEntity>.EQ(e => e.Id, id);
            var entityNew = collection.FindOne(query);
            Assert.AreEqual(entityNew.Id, entity.Id);
            Assert.AreEqual(entity.StringTest, entityNew.StringTest);

            //Assert.AreEqual(entity.DateTest,entityNew.DateTest);
            // This gives one day error:
            //  Expected: 2013-10-13 00:00:00.000
            //  But was:  2013-10-12 22:00:00.000
            //Assert.AreEqual(entity.DateTest.ToLocalTime(),entityNew.DateTest.ToLocalTime());
            // This gives a 2 hours error.
            //   Expected: 2013-10-13 02:00:00.000
            //   But was:  2013-10-13 00:00:00.000
            Assert.AreEqual(entity.DateTest, entityNew.DateTest.ToLocalTime());
        }
    }
 }

如果我取消对任何Asserts.AreEqual的评论,则会出现错误(在下面评论).

If I uncomment any of the Asserts.AreEqual I get an error (commented below).

保存的实体为:

{
"_id" : "1",
"StringTest" : "Test",
"DateTest" : ISODate("2013-10-12T22:00:00Z")
 }

我知道这可能与ISODate和UTC有关(我在UTC + 1中),但是我有点恼火,因为我的日期保存在集合中有一天的差异,并且要求我在任何时候都转换为localTime提取一些带有日期的数据.

I understand that this could be something related to ISODate and UTC (I am in UTC+1) but I a bit annoyed that my dates are saved with one day difference in the collection and requires me to convert to localTime any time I fetch some data with dates.

此行为的原因是什么,有什么办法可以避免呢?

What is the reason for this behaviour and is there a way to avoid it?

推荐答案

在大多数情况下,您希望将UTC日期时间存储在数据库中,因此DateTime应该构造为:-

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.

如果不指定DateTimeKind,则可能要碰碰运气. MongoDB似乎假定它是本地的,并将其转换为数据库中的UTC.

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.

还请注意,MongoDB DateTime值的精度低于.NET DateTime值.如果要存储任意的DateTime值并以仍然匹配的方式返回它们,则需要在存储它们之前将它们四舍五入到最接近的毫秒数.

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.

如果您确实想存储本地时间,建议您从DateTime切换到DateTimeOffset,并将其序列化为UTC DateTime的长Tick值和偏移量的值.

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.

请注意,除非您存储在获取DateTime值时计算的偏移量,否则用于转换为LocalTime的.NET方法本质上是无用的,因为它们不知道夏令时开始的时间,甚至也不知道夏令时的开始时间. DateTime值来自.总体而言,.NET DateTime处理还有很多不足之处,并且包含许多误导性方法,这些方法声称有帮助,但实际上没有帮助.

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.

这篇关于Mongodb C#驱动程序和ISODate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆