Created
November 25, 2010 00:49
-
-
Save rstam/714723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MongoDB.Bson; | |
using MongoDB.Bson.DefaultSerializer; | |
using MongoDB.Bson.IO; | |
using MongoDB.Bson.Serialization; | |
using MongoDB.Driver; | |
using MongoDB.Driver.Builders; | |
namespace ConsoleApplication1 { | |
public class Model { | |
public String Id { get; set; } | |
public string Name { get; set; } | |
public DateTime DateTimeProperty { get; set; } | |
} | |
public static class Program { | |
public static void Main(string[] args) { | |
// serialization configuration must be done before starting to use MongoDB | |
var localTimeOption = new DateTimeSerializationOptions { Kind = DateTimeKind.Local }; | |
BsonClassMap.RegisterClassMap<Model>(cm => { | |
cm.MapIdProperty(c => c.Id); | |
cm.MapProperty(c => c.Name); | |
cm.MapProperty(c => c.DateTimeProperty) | |
.SetSerializationOptions(localTimeOption); | |
}); | |
var server = MongoServer.Create(); | |
var database = server["test"]; | |
var collection = database.GetCollection<Model>("model"); // default document type is Model | |
collection.RemoveAll(); | |
var m1 = new Model { | |
Id = Guid.NewGuid().ToString(), | |
DateTimeProperty = DateTime.Now, | |
Name = "test" | |
}; | |
collection.Save(m1); | |
var query = Query.EQ("_id", m1.Id); | |
var m2 = collection.FindOne(query); | |
if (m1.DateTimeProperty.ToShortTimeString() != m2.DateTimeProperty.ToShortTimeString()) | |
throw new Exception(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment