public Task <DeleteResult> DeleteAsync <T>(string Name, Type Type, T Document) { if (MongoMapperTransaction.InTransaction && !MongoMapperTransaction.Commiting) { MongoMapperTransaction.AddToQueue(OperationType.Delete, Type, Document); Task.FromResult(true); } var mongoMapperIdeable = Document as IMongoMapperIdeable; Debug.Assert(mongoMapperIdeable != null, "mongoMapperIdeable != null"); if (mongoMapperIdeable.m_id == default(long)) { mongoMapperIdeable.m_id = Finder.Instance.FindIdByKey <T>(Type, MongoMapperHelper.GetPrimaryKey(Type). ToDictionary( KeyField => KeyField, KeyField => ReflectionUtility. GetPropertyValue( this, KeyField))); } var query = Builders <T> .Filter.Eq("_id", mongoMapperIdeable.m_id); return(CollectionsManager.GetCollection <T>(Type.Name).DeleteOneAsync(query)); }
private void AddProjetion() { ProjectionDefinition <T> fields = null; if (_includedFields.Any()) { var includeFieldList = MongoMapperHelper.ConvertFieldName(typeof(T).Name, _includedFields).ToList(); fields = this.Project.Include(includeFieldList.First()); foreach (var field in includeFieldList.Skip(1)) { fields = this.Project.Include(field); } } if (_excludedFields.Any()) { var excludeFieldList = MongoMapperHelper.ConvertFieldName(typeof(T).Name, _excludedFields).ToList(); fields = this.Project.Exclude(excludeFieldList.First()); foreach (var field in excludeFieldList.Skip(1)) { fields = this.Project.Exclude(field); } } if (fields != null) { Cursor.Project <T>(fields); } }
public static IEnumerable <string> GetCollentionNames(string DbName) { var colNames = new List <string>(); MongoMapperHelper.Db(DbName).ListCollectionsAsync().Result.ForEachAsync(F => colNames.Add(F["name"].AsString)); return(colNames.Where(C => !C.ToUpper().StartsWith("SYSTEM"))); }
public static void BuildSchema(Assembly Assembly, string ClassName) { List <Type> types = String.IsNullOrEmpty(ClassName) ? Assembly.GetTypes().Where(T => T.BaseType != null && T.BaseType.Name == "MongoMapper`1").ToList() : Assembly.GetTypes().Where(T => T.BaseType != null && T.BaseType.Name == "MongoMapper`1" && T.Name == ClassName).ToList(); foreach (Type type in types) { MongoMapperHelper.RebuildClass(type, true); } }
public static bool CollectionExists(string Name) { var filter = new BsonDocument("name", Name); //filter by collection name var collections = MongoMapperHelper.Db(Name).ListCollectionsAsync(new ListCollectionsOptions { Filter = filter }).GetAwaiter().GetResult().ToListAsync().Result; //check for existence return(collections.Any()); }
public T FindByKey <T>(params object[] Values) { List <string> fields = MongoMapperHelper.GetPrimaryKey(typeof(T)).ToList(); var keyValues = new Dictionary <string, object>(); for (int i = 0; i < fields.Count; i++) { string field = fields[i].ToUpper() == "m_id".ToUpper() ? "_id" : fields[i]; keyValues.Add(field, Values[i]); } return(FindObjectByKey <T>(keyValues)); }
private long FindAndModifyResult(string ObjName) { var result = MongoMapperHelper.Db("Counters", true) .GetCollection <BsonDocument>("Counters") .FindOneAndUpdateAsync( Builders <BsonDocument> .Filter.Eq("document", ObjName), Builders <BsonDocument> .Update.Inc("last", (long)1), new FindOneAndUpdateOptions <BsonDocument>() { IsUpsert = true, ReturnDocument = ReturnDocument.After } ).Result; return(result["last"].AsInt64); }
public static void DeteleExistingIndexesAndBuildNewOnes(Assembly Assembly, string ClassName) { List <Type> types = String.IsNullOrEmpty(ClassName) ? Assembly.GetTypes().Where(T => T.BaseType != null && T.BaseType.Name == "MongoMapper`1").ToList() : Assembly.GetTypes().Where(T => T.BaseType != null && T.BaseType.Name == "MongoMapper`1" && T.Name == ClassName).ToList(); foreach (Type type in types) { Console.WriteLine("BEGIN " + type.Name); var indexes = MongoMapperHelper.GetExistinIndexNames(type); foreach (var index in indexes.Where(I => I != "_id_")) { Console.WriteLine("DELETING INDEX IN" + type.Name + " => " + index); CollectionsManager.GetCollection <BsonDocument>(type.Name).Indexes.DropOne(index); } MongoMapperHelper.CreateIndexes(type); Console.WriteLine("END " + type.Name); } }
public static IMongoCollection <T> GetPrimaryCollection <T>(string Name) { Name = GetCollectioName(Name); string key = string.Format("{0}|{1}|{2}", Name, typeof(T).FullName, Configuration.ConfigManager.GetConfigurationKey()); if (PrimaryCollections.ContainsKey(key)) { return((IMongoCollection <T>)PrimaryCollections[key]); } lock (LockObject) { if (!PrimaryCollections.ContainsKey(key)) { var collection = MongoMapperHelper.Db(Name, true).GetCollection <T>(Name); PrimaryCollections.Add(key, collection); } return((IMongoCollection <T>)PrimaryCollections[key]); } }
public static FilterDefinition <T> RegEx(string ObjName, string FieldName, string expresion) { FieldName = MongoMapperHelper.ConvertFieldName(ObjName, FieldName); return(Builders <T> .Filter.Regex(FieldName, expresion)); }
public static FilterDefinition <T> Ne(string ObjName, string FieldName, object Value) { object defaultValue = MongoMapperHelper.GetFieldDefaultValue(ObjName, FieldName); FieldName = MongoMapperHelper.ConvertFieldName(ObjName, FieldName); FilterDefinition <T> query = null; Type type = Value.GetType(); if (type.BaseType != null && type.BaseType.Name == "Enum") { type = typeof(int); } if (type == typeof(DateTime)) { query = Builders <T> .Filter.Ne(FieldName, (DateTime)Value); if (defaultValue != null && (DateTime)defaultValue != (DateTime)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(int)) { query = Builders <T> .Filter.Ne(FieldName, (int)Value); if (defaultValue != null && (int)defaultValue != (int)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(string)) { query = Builders <T> .Filter.Ne(FieldName, (string)Value); if (defaultValue != null && (string)defaultValue != (string)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(long)) { query = Builders <T> .Filter.Ne(FieldName, (long)Value); if (defaultValue != null && (long)defaultValue != (long)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(bool)) { query = Builders <T> .Filter.Ne(FieldName, (bool)Value); if (defaultValue != null && (bool)defaultValue != (bool)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(double)) { query = Builders <T> .Filter.Ne(FieldName, (double)Value); if (defaultValue != null && (double)defaultValue != (double)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } return(query); }
public static FilterDefinition <T> Eq(string ObjName, string FieldName, object Value) { object defaultValue = MongoMapperHelper.GetFieldDefaultValue(ObjName, FieldName); FieldName = MongoMapperHelper.ConvertFieldName(ObjName, FieldName); FilterDefinition <T> query = null; Type type = Value.GetType(); if (type == typeof(BsonNull)) { query = Builders <T> .Filter.Eq(FieldName, BsonNull.Value); return(query); } if (type.BaseType != null && type.BaseType.Name == "Enum") { type = typeof(int); if ((int)Value < 0) { return(new BsonDocument()); } } if (type == typeof(string)) { bool isLike = false; string txtValue = Value.ToString(); if (txtValue.Trim() == "%") { return(new BsonDocument()); } if (txtValue.StartsWith("%") && txtValue.EndsWith("%")) { Value = String.Format("/{0}/", txtValue); isLike = true; } else if (txtValue.StartsWith("%")) { Value = String.Format("/{0}$/", txtValue); isLike = true; } else if (txtValue.EndsWith("%")) { Value = String.Format("/^{0}/", txtValue); isLike = true; } if (isLike) { query = Builders <T> .Filter.Regex(FieldName, new BsonRegularExpression(string.Format("{0}i", Value.ToString().Replace("%", "")))); return(query); } } if (type == typeof(DateTime)) { query = Builders <T> .Filter.Eq(FieldName, (DateTime)Value); if (defaultValue != null && (DateTime)defaultValue == (DateTime)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(int)) { query = Builders <T> .Filter.Eq(FieldName, (int)Value); if (defaultValue != null && (int)defaultValue == (int)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(string)) { query = Builders <T> .Filter.Eq(FieldName, (string)Value); if (defaultValue != null && (string)defaultValue == (string)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(long)) { query = Builders <T> .Filter.Eq(FieldName, (long)Value); if (defaultValue != null && (long)defaultValue == (long)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(bool)) { query = Builders <T> .Filter.Eq(FieldName, (bool)Value); if (defaultValue != null && (bool)defaultValue == (bool)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(double)) { query = Builders <T> .Filter.Eq(FieldName, (double)Value); if (defaultValue != null && (double)defaultValue == (double)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } else if (type == typeof(Guid)) { query = Builders <T> .Filter.Eq(FieldName, (Guid)Value); if (defaultValue != null && (Guid)defaultValue == (Guid)Value) { query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false)); } } return(query); }
protected MongoMapper() { _classType = GetType(); BsonDefaults.MaxDocumentSize = ConfigManager.MaxDocumentSize(_classType.Name) * 1024 * 1024; MongoMapperHelper.RebuildClass(_classType, false); }
private Dictionary <string, object> GetPrimaryKeyValues() { return(MongoMapperHelper.GetPrimaryKey(_classType).ToDictionary( KeyField => KeyField, KeyField => ReflectionUtility.GetPropertyValue(this, KeyField))); }
internal static void CreateIndexes(Type ClassType) { var existingIndexNames = GetExistinIndexNames(ClassType); foreach (string index in GetIndexes(ClassType)) { if (index.StartsWith("2D|")) { var mongoIndex = Builders <BsonDocument> .IndexKeys.Geo2D(MongoMapperHelper.ConvertFieldName(ClassType.Name, index.Split('|')[1]).Trim()); var indexName = "2D" + "_" + index.Split('|')[1]; if (!existingIndexNames.Contains(indexName)) { Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName); CollectionsManager.GetCollection <BsonDocument>(ClassType.Name) .Indexes.CreateOneAsync(mongoIndex, new CreateIndexOptions() { Name = indexName }) .GetAwaiter() .GetResult(); } } else if (index.StartsWith("2DSphere|")) { var indexName = "2DSphere" + "_" + index.Split('|')[1]; if (!existingIndexNames.Contains(indexName)) { Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName); var mongoIndex = Builders <BsonDocument> .IndexKeys.Geo2DSphere( MongoMapperHelper.ConvertFieldName(ClassType.Name, index.Split('|')[1]).Trim()); CollectionsManager.GetCollection <BsonDocument>(ClassType.Name) .Indexes.CreateOneAsync(mongoIndex, new CreateIndexOptions() { Name = indexName }) .GetAwaiter() .GetResult(); } } else { var indexFieldnames = MongoMapperHelper.ConvertFieldName(ClassType.Name, index.Split(',').ToList()).Select(IndexField => IndexField.Trim()); var fieldnames = indexFieldnames as IList <string> ?? indexFieldnames.ToList(); if (fieldnames.Any()) { var indexName = "IX" + "_" + string.Join("_", fieldnames); if (!existingIndexNames.Contains(indexName)) { Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName); var indexFields = Builders <BsonDocument> .IndexKeys.Ascending(fieldnames.First()); indexFields = fieldnames.Skip(1) .Aggregate(indexFields, (Current, FieldName) => Current.Ascending(FieldName)); CollectionsManager.GetCollection <BsonDocument>(ClassType.Name) .Indexes.CreateOneAsync(indexFields, new CreateIndexOptions() { Name = indexName }) .GetAwaiter() .GetResult(); } } } } string[] pk = GetPrimaryKey(ClassType).ToArray(); if (pk.Count(K => K == "m_id") == 0) { var indexFieldnames = MongoMapperHelper.ConvertFieldName(ClassType.Name, pk.ToList()).Select(PkField => PkField.Trim()); var fieldnames = indexFieldnames as IList <string> ?? indexFieldnames.ToList(); if (fieldnames.Any()) { var indexName = "PK_" + string.Join("_", fieldnames); if (!existingIndexNames.Contains(indexName)) { Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName); var indexFields = Builders <BsonDocument> .IndexKeys.Ascending(fieldnames.First()); indexFields = fieldnames.Skip(1) .Aggregate(indexFields, (Current, FieldName) => Current.Ascending(FieldName)); CollectionsManager.GetCollection <BsonDocument>(ClassType.Name) .Indexes.CreateOneAsync(indexFields, new CreateIndexOptions() { Unique = true, Name = indexName }) .GetAwaiter() .GetResult(); } } } string ttlIndex = GetTTLIndex(ClassType); if (ttlIndex != string.Empty) { var tmpIndex = ttlIndex.Split(','); var indexName = "TTL_" + tmpIndex[0].Trim(); if (!existingIndexNames.Contains(indexName)) { Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName); var keys = Builders <BsonDocument> .IndexKeys.Ascending(tmpIndex[0].Trim()); CollectionsManager.GetCollection <BsonDocument>(ClassType.Name).Indexes.CreateOneAsync( keys, new CreateIndexOptions() { Name = indexName, ExpireAfter = TimeSpan.FromSeconds(int.Parse(tmpIndex[1].Trim())) }) .GetAwaiter() .GetResult(); } } }