public static MongoCollection <T> GetCollection <T>(string collectionName) where T : new() { try { string ns; var index = collectionName.LastIndexOf('.'); if (index >= 0) { ns = collectionName; } else { var t = new T(); var tt = t.GetType(); ns = tt.Namespace; index = ns.LastIndexOf('.'); if (index >= 0) { ns = ns.Substring(0, index) + $".{collectionName}"; } } MongoCollectionInfo collectionInfo = null; foreach (var conn in Settings.Instance.Connections) { foreach (var dbInfo in conn.Databases) { foreach (var collInfo in dbInfo.Collections) { if ("MongoSharp.Query." + collInfo.Namespace == ns) { collectionInfo = collInfo; break; } } } } if (collectionInfo == null) { throw new Exception($"Collection '{collectionName}' not found."); } var client = new MongoClient(collectionInfo.Database.Connection.GetConnectionString(collectionInfo.Database.Name)); var server = client.GetServer(); var database = server.GetDatabase(collectionInfo.Database.Name); var collection = database.GetCollection <T>(collectionInfo.Name); return(collection); } catch (Exception e) { throw new Exception($"Error getting collection for type '{typeof(T).ToString()}'.", e); } }
public MongoCollectionInfo GetCollectionInfo(string collectionName) { if (Collections != null && Collections.Count > 0) { MongoCollectionInfo collectionInfo = Collections.Find(col => col.Name.Equals(collectionName)); return(collectionInfo); } return(null); }
public MongoCollectionInfo AddCollection(string name) { var dbInfo = new MongoCollectionInfo { Name = name, Database = this }; Collections.Add(dbInfo); return(dbInfo); }
public MongoCollectionModelInfo SetCollectionModel(string collectionName, string modelCode, string rootClassName, bool isAutoGenerated, int sampleSize, decimal samplePercent) { if (Collections != null && Collections.Count > 0) { var collection = Collections.Find(col => col.Name.Equals(collectionName)); if (collection != null) { collection.Models.Clear(); collection.Models.Add(new MongoCollectionModelInfo { Collection = collection, ModelCode = modelCode, RootClassName = rootClassName, IsAutoGenerated = isAutoGenerated, SampleSize = sampleSize, SamplePercent = samplePercent }); return(collection.Models[0]); } } if (Collections == null) { Collections = new List <MongoCollectionInfo>(); } var newCollection = new MongoCollectionInfo { Name = collectionName, Database = this, Models = new List <MongoCollectionModelInfo> { new MongoCollectionModelInfo { Collection = null, ModelCode = modelCode, RootClassName = rootClassName, IsAutoGenerated = isAutoGenerated, SampleSize = sampleSize, SamplePercent = samplePercent } } }; newCollection.Models[0].Collection = newCollection; Collections.Add(newCollection); return(newCollection.Models[0]); }
public static MongoCollection <T> GetCollection <T>() where T : new() { try { var t = new T(); var tt = t.GetType(); var ns = tt.Namespace; MongoCollectionInfo collectionInfo = null; foreach (var conn in Settings.Instance.Connections) { foreach (var dbInfo in conn.Databases) { foreach (var collInfo in dbInfo.Collections) { if ("MongoSharp.Query." + collInfo.Namespace == ns) { collectionInfo = collInfo; break; } } } } if (collectionInfo == null) { throw new Exception("Collection not found."); } var client = new MongoClient(collectionInfo.Database.Connection.GetConnectionString(collectionInfo.Database.Name)); var server = client.GetServer(); var database = server.GetDatabase(collectionInfo.Database.Name); var collection = database.GetCollection <T>(collectionInfo.Name); return(collection); } catch (Exception e) { throw new Exception($"Error getting collection for type '{typeof(T).ToString()}'.", e); } }
private MongoCollectionModelInfo GetModel(MongoDatabaseInfo databaseInfo, string collectionName) { MongoCollectionInfo collectionInfo = databaseInfo.GetCollectionInfo(collectionName); if (collectionInfo != null && collectionInfo.HasModel) { // Model already exists for this collection. return(collectionInfo.Models[0]); } // Generate and save model code. var collection = databaseInfo.GetCollection(collectionName); var doc = collection.FindOne(); if (doc == null) { throw new Exception( $"Collection '{collectionName}' is empty. Unable to determine schema from first document"); } var schemaInfo = MongoCollectionSchemaStore.GetSchemaDocument(databaseInfo, collectionName); var classes = new BsonDocumentConverter().ToCSharpClassDeclarations(schemaInfo.SchemaDocument, collectionName); var sb = new StringBuilder(); foreach (var classSyntax in classes) { sb.AppendLine(classSyntax); } string modelCode = sb.ToString(); MongoCollectionModelInfo newModel = databaseInfo.SetCollectionModel(collectionName, modelCode, "Doc0", true, (int)schemaInfo.SampleSize, schemaInfo.SamplePercent); Settings.Instance.Save(); return(newModel); }