public void Test() { var configure = new MongoConfigurationBuilder(); configure.ConnectionString(cs => { cs.Pooled = true; }); configure.Mapping(mapping => { mapping.DefaultProfile(p => { p.AliasesAreCamelCased(); p.CollectionNamesAreCamelCasedAndPlural(); }); mapping.Map<Person>(m => { m.CollectionName("people"); m.Member(x => x.Age).Alias("age"); m.Member(x => x.Name).Alias("name").DefaultValue("something").Ignore(); }); }); }
public MongoUnitOfWorkFactory(string connection) { var builder = new MongoConfigurationBuilder(); builder.ConnectionString(connection); builder.Mapping(m => m.DefaultProfile(profile => profile .UseIdUnsavedValueConvention(new UnsavedIdConvention()))); _mongo = new Mongo(builder.BuildConfiguration()); }
private static MongoConfiguration BuildMongoConfiguration(ITypeCatalog domainEventTypeCatalog, string connectionString) { var configurationBuilder = new MongoConfigurationBuilder(); configurationBuilder.ConnectionString(connectionString); configurationBuilder.Mapping(mapping => { mapping.DefaultProfile(profile => profile.SubClassesAre(t => t.IsSubclassOf(typeof(DomainEvent)))); domainEventTypeCatalog .GetDerivedTypes(typeof(DomainEvent)) .ToList() .ForEach(type => MapEventType(type, mapping)); }); return configurationBuilder.BuildConfiguration(); }
public static MongoDB.Mongo GetDB() { var config = new MongoConfigurationBuilder(); config.Mapping(mapping => { mapping.DefaultProfile(profile => { profile.SubClassesAre(x => x.IsSubclassOf(typeof(Module))); }); mapping.Map<ContentModule>(); mapping.Map<ListModule>(); }); config.ConnectionString("mongodb://localhost"); MongoDB.Mongo mongo = new MongoDB.Mongo(config.BuildConfiguration()); mongo.Connect(); return mongo; }
public static void Main(string[] args) { var config = new MongoConfigurationBuilder(); // COMMENT OUT FROM HERE config.Mapping(mapping => { mapping.DefaultProfile(profile => { profile.SubClassesAre(t => t.IsSubclassOf(typeof(MyClass))); }); mapping.Map<MyClass>(); mapping.Map<SubClass>(); }); // TO HERE config.ConnectionString("Server=127.0.0.1"); using (Mongo mongo = new Mongo(config.BuildConfiguration())) { mongo.Connect(); try { var db = mongo.GetDatabase("TestDb"); var collection = db.GetCollection<MyClass>(); MyClass square = new MyClass() { Corners = 4, Name = "Square" }; MyClass circle = new MyClass() { Corners = 0, Name = "Circle" }; SubClass sub = new SubClass() { Name = "SubClass", Corners = 6, Ratio = 3.43 }; collection.Save(square); collection.Save(circle); collection.Save(sub); var superclass = (from item in db.GetCollection<MyClass>("MyClass").Linq() where item.Corners > 1 select item.Corners).ToList(); var subclass = (from item in db.GetCollection<SubClass>("MyClass").Linq() where item.Ratio > 1 select item.Corners).ToList(); Console.WriteLine("Count by LINQ on typed collection: {0}", collection.Linq().Count(x => x.Corners > 1)); Console.WriteLine("Count by LINQ on typed collection2: {0}", db.GetCollection<SubClass>().Linq().Count(x => x.Corners > 1)); //Console.WriteLine("Count by LINQ on typed collection3: {0}", db.GetCollection<SubClass>().Count(new { Corners = Op.GreaterThan(1) })); Console.WriteLine("Count on typed collection: {0}", collection.Count(new { Corners = Op.GreaterThan(1) })); var coll = db.GetCollection("MyClass"); var count = coll.Count(new Document("Corners", Op.GreaterThan(1))); Console.WriteLine("Count: {0}", count); Console.ReadKey(); } finally { mongo.Disconnect(); } } //var main = new MainClass(); //main.Setup(); //main.Run(); }