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(); } } }