Skip to content

Instantly share code, notes, and snippets.

@rstam
Created November 21, 2010 02:30
Show Gist options
  • Save rstam/708380 to your computer and use it in GitHub Desktop.
Save rstam/708380 to your computer and use it in GitHub Desktop.
using System;
using MongoDB.Bson;
using MongoDB.Bson.DefaultSerializer;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace ConsoleApplication1 {
public class Model {
public DateTime DateTimeProperty { get; set; }
}
public static class Program {
public static void Main(string[] args) {
var server = MongoServer.Create();
var database = server["test"];
var collection = database["test"];
collection.RemoveAll();
var localTime = new DateTimeSerializationOptions { Kind = DateTimeKind.Local };
BsonClassMap.RegisterClassMap<Model>(cm => {
cm.MapProperty(c => c.DateTimeProperty)
.SetSerializationOptions(localTime);
});
var m1 = new Model { DateTimeProperty = DateTime.Now };
var bson = m1.ToBson();
var m2 = BsonSerializer.Deserialize<Model>(bson);
var document = BsonSerializer.Deserialize<BsonDocument>(bson);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment