13

我有一个将日期时间存储为 UTC 的 Mongo C# 实现。

MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions options = 
    MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions.UtcInstance;

var serializer = 
    new MongoDB.Bson.Serialization.Serializers.DateTimeSerializer(options);

MongoDB.Bson.Serialization.BsonSerializer.RegisterSerializer(
    typeof(DateTime),
    serializer);

我还需要将用户本地时区与 UTC 一起存储。为了解释,我有两个属性,就像

DateTime WorkItemToCompleteBy{get; set;}
[BsonDateTimeOptions(Kind = DateTimeKind.Unspecified)]
DateTime WorkItemToCompleteByLocal{get; set;}

我想将澳大利亚/美国/印度/其他时间存储在 Local 属性中,并将各自的 UTC 值存储在另一个属性中。由于要处理几十个时区,我有将 UTC 转换为所需时区并将其存储在 WorkItemToCompleteByLocal 属性中的代码。我希望 Mongo 将这个值“原样”存储并返回给我。问题是 Mongo 总是将其存储为 ISODate 并将值转换为 Utc 版本。解释。如果 UTC 为 0730 小时,我将布里斯班时间计算为 1730 小时并将其设置为 WorkitemToCompleteByLocal,它们将保存为

"WorkItemToCompleteBy" : ISODate("2013-06-05T07:30:00Z"),
"WorkItemToCompleteByLocal" : ISODate("2013-06-05T12:00:00Z"),

Mongo 将提供的时间解释为本地时间,服务器位于印度,并将其转换为 1200 小时的等效 UTC。虽然它将值检索为 1730 (IST Albeit),但它违背了我的目的,并阻止我在 Mongo 上运行任何基于本地时间的查询。我没有想法。感谢任何帮助以帮助存储 WorkItemToCompleteByLocal 日期“原样”而不进行修改

4

3 回答 3

23

这是我强制 MongoDB 存储原始值并忽略 DateTime 对象中的 DateTimeKind 属性的方式。

这可能不适用于您的业务逻辑,但由于我们的特殊原因,这对我们有意义。

BsonSerializer.RegisterSerializer(typeof(DateTime), new MyMongoDBDateTimeSerializer());


public class MyMongoDBDateTimeSerializer : DateTimeSerializer
{
    //  MongoDB returns datetime as DateTimeKind.Utc, which cann't be used in our timezone conversion logic
    //  We overwrite it to be DateTimeKind.Unspecified
    public override object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, System.Type nominalType, MongoDB.Bson.Serialization.IBsonSerializationOptions options)
    {
        var obj = base.Deserialize(bsonReader, nominalType, options);
        var dt = (DateTime) obj;
        return new DateTime(dt.Ticks, DateTimeKind.Unspecified);
    }

    //  MongoDB returns datetime as DateTimeKind.Utc, which cann't be used in our timezone conversion logic
    //  We overwrite it to be DateTimeKind.Unspecified
    public override object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, Type actualType, MongoDB.Bson.Serialization.IBsonSerializationOptions options)
    {
        var obj = base.Deserialize(bsonReader, nominalType, actualType, options);
        var dt = (DateTime)obj;
        return new DateTime(dt.Ticks, DateTimeKind.Unspecified);
    }

    //  MongoDB stores all datetime as Utc, any datetime value DateTimeKind is not DateTimeKind.Utc, will be converted to Utc first
    //  We overwrite it to be DateTimeKind.Utc, becasue we want to preserve the raw value
    public override void Serialize(MongoDB.Bson.IO.BsonWriter bsonWriter, System.Type nominalType, object value, MongoDB.Bson.Serialization.IBsonSerializationOptions options)
    {
        var dt = (DateTime) value;
        var utcValue = new DateTime(dt.Ticks, DateTimeKind.Utc);
        base.Serialize(bsonWriter, nominalType, utcValue, options);
    }
}
于 2013-09-06T04:30:18.030 回答
10

新版 C# 驱动 =>

    public class MyMongoDBDateTimeSerializer : DateTimeSerializer
{
    //  MongoDB returns datetime as DateTimeKind.Utc, which cann't be used in our timezone conversion logic
    //  We overwrite it to be DateTimeKind.Unspecified
    public override DateTime Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        var obj = base.Deserialize(context, args);
        return new DateTime(obj.Ticks, DateTimeKind.Unspecified);
    }

    //  MongoDB stores all datetime as Utc, any datetime value DateTimeKind is not DateTimeKind.Utc, will be converted to Utc first
    //  We overwrite it to be DateTimeKind.Utc, becasue we want to preserve the raw value
    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTime value)
    {
        var utcValue = new DateTime(value.Ticks, DateTimeKind.Utc);
        base.Serialize(context, args, utcValue);
    }
}
于 2016-08-22T08:17:28.053 回答
9

在 .net 驱动程序版本 2.4 中,使用以下命令:

using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

BsonSerializer.RegisterSerializer(DateTimeSerializer.LocalInstance);

这样,日期时间值将以 kind=utc 形式存储在 db 中,但以本地类型反序列化。

于 2017-09-06T02:47:22.187 回答