How to map date field in EDT format to C# poco class with datetime property

408 views
Skip to first unread message

Rajendra Gupta

unread,
May 11, 2016, 12:43:37 PM5/11/16
to mongodb-user
Hello,

I have stored my data in MobgoDB like.

db.requests.insert({

"_id" : "76f62a60-001b-11e6-8c47-02421d3a8dc5",

"controlnumber" : "2182048",

"created-date" : "Fri Mar 25 11:04:33 EDT 2016"

})



I have a POCO c# class like

public class RequestModel
    {
        public string Id { get; set; }
        public string ControlNumber { get; set; }        
        public DateTime CreatedDate { get; set; }  
    }

And want to map this class with MongoDB document with AutoMap as


BsonClassMap.RegisterClassMap<RequestModel>(map =>
            {
                map.AutoMap();
                map.SetIgnoreExtraElements(true);
                map.GetMemberMap(x => x.Id).SetElementName("_id");
                map.GetMemberMap(x => x.ControlNumber).SetElementName("controlnumber");                
                map.GetMemberMap(x => x.CreatedDate).SetElementName("created-date");                 
            });

When i keep all the properties are of string then its working fine however i facing issue while taking CreatedDate as DateTime type.

I appreciate, if anybody can help me on the approach to Map the DateTime field in Poco class.

  

Wan Bachtiar

unread,
May 31, 2016, 3:03:29 AM5/31/16
to mongodb-user

When i keep all the properties are of string then its working fine however i facing issue while taking CreatedDate as DateTime type.

Hi Rajendra,

If it’s possible, I would recommend to store the value of CreatedDate in BSON Date UTC format instead of string.

Having said that, if you would like to map CreatedDate from String to DateTime, you can try to use Serialization. Write a custom serialiser class inheriting SerializerBase.cs, to convert string to datetime (using DateTime ). For example:

class DateCreationSerializer : SerializerBase<DateTime>
{
    public override DateTime Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        string stringdate = context.Reader.ReadString();
        return DateTime.ParseExact(stringdate.Replace("EDT", "-07:00"), "ddd MMM d h:mm:s zzz yyyy", CultureInfo.InvariantCulture);
    }
    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTime value)
    {
        context.Writer.WriteString(value.ToString("ddd MMM d h:mm:s EDT yyyy"));
    }
}

You can then set the serialiser class when you register the mapping, as below:

BsonClassMap.RegisterClassMap<RequestModel>(map =>
{
    map.AutoMap();
    map.SetIgnoreExtraElements(true);
    map.GetMemberMap(x => x.Id).SetElementName("_id");
    map.GetMemberMap(x => x.ControlNumber).SetElementName("controlnumber"
);
    map.GetMemberMap(x => x.CreatedDate).SetElementName("created-date").SetSerializer(new DateCreationSerializer());
});

Also see BsonSerializerTests.cs as a great reference of serialisation.

The above snippet was tested using MongoDB v3.2, .Net v4.5.2 and mongo-csharp-driver v2.2.x

You may also be interested to check out the TimeZone class to properly convert EDT.

Best Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages