MongoDB时区问题的处理方案

使用MongoDB的都知道MongoDB存储时间类型存在时区问题

1、简单处理方法:

      把日期类型改为字符串存储

2、对实体类的读写方法进行逻辑处理(要定义时区标识并自定义读写方法)

3、利用MongoDB.Bson的序列化实现(我的处理方案

DateTime类型:

public class MongoDateTimeSerializer : IBsonSerializer<DateTime>
    {
        public Type ValueType => typeof(DateTime);

        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            var bsonType = context.Reader.CurrentBsonType;
            if (bsonType == BsonType.DateTime)
            {
                return new DateTime(1970, 1, 1).AddMilliseconds(context.Reader.ReadDateTime());
            }
            else {

                throw new Exception("类型错误");
            
            }

        }

        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            if (value != null)
            {
                context.Writer.WriteDateTime(Convert.ToInt64((Convert.ToDateTime(value) - new DateTime(1970, 1, 1)).TotalMilliseconds));
            }
            else
            {
                context.Writer.WriteNull();
            }
        }


        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTime value)
        {
            context.Writer.WriteDateTime(Convert.ToInt64((value - new DateTime(1970, 1, 1)).TotalMilliseconds));
        }

        DateTime IBsonSerializer<DateTime>.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonType = context.Reader.CurrentBsonType;
            if (bsonType == BsonType.DateTime)
            {
                return new DateTime(1970, 1, 1).AddMilliseconds(context.Reader.ReadDateTime());
            }
            else
            {
                throw new Exception("类型错误");
            }
        }
    }

DateTime?类型:

public class MongoNullDateTimeSerializer : IBsonSerializer<DateTime?>
    {
        public Type ValueType => typeof(DateTime?);

        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            var bsonType = context.Reader.CurrentBsonType;
            if (bsonType == BsonType.DateTime)
            {
                return new DateTime(1970, 1, 1).AddMilliseconds(context.Reader.ReadDateTime());
            }
            else if (bsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }
            else
            {
                throw new Exception("类型错误");
            }
        }

        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            if (value != null)
            {
                context.Writer.WriteDateTime(Convert.ToInt64((Convert.ToDateTime(value) - new DateTime(1970, 1, 1)).TotalMilliseconds));
            }
            else
            {
                context.Writer.WriteNull();
            }
        }

        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTime? value)
        {
            if (value != null)
            {
                context.Writer.WriteDateTime(Convert.ToInt64((value.Value - new DateTime(1970, 1, 1)).TotalMilliseconds));
            }
            else
            {
                context.Writer.WriteNull();
            }
        }

        DateTime? IBsonSerializer<DateTime?>.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonType = context.Reader.CurrentBsonType;
            if (bsonType == BsonType.DateTime)
            {
                return new DateTime(1970, 1, 1).AddMilliseconds(context.Reader.ReadDateTime());
            }
            else if (bsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }
            else
            {
                throw new Exception("类型错误");
            }
        }
    }

项目启动时注册类型的序列化实现

 public override void ConfigureServices(ServiceConfigurationContext context)
        {
            BsonSerializer.RegisterSerializer(typeof(DateTime), new MongoDateTimeSerializer() );
            BsonSerializer.RegisterSerializer(typeof(DateTime?), new MongoNullDateTimeSerializer());
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值