MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

Mongo . में यूटीसी और स्थानीय डेटाटाइम संग्रहित करना

इस तरह मैं मोंगोडीबी को कच्चे मूल्य को स्टोर करने के लिए मजबूर करता हूं, और डेटटाइम ऑब्जेक्ट में डेटटाइमकिंड विशेषता को अनदेखा करता हूं।

यह आपके व्यावसायिक तर्क पर लागू नहीं हो सकता है, लेकिन हमारे विशेष कारणों से हमारे लिए समझ में आता है।

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);
    }
}


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. क्या एक Mongo $नियर रिटर्न दस्तावेज़ जिसके लिए मल्टीपॉइंट में कोई बिंदु सीमा के भीतर है?

  2. मोंगोडीबी $बाइनरीसाइज

  3. घातक त्रुटि:mongodb php ड्राइवर 1.1.2 और PHP 7.0.2 - Laravel 5.1 का उपयोग करते समय क्लास 'MongoDate' नहीं मिला

  4. MongoDB एकत्रीकरण का कुशल पृष्ठांकन?

  5. JasperReports लाइब्रेरी और MongoDB के साथ PDF जेनरेट करें