diff --git a/MongoDbGenericRepository/BaseMongoDbRepository.cs b/MongoDbGenericRepository/BaseMongoDbRepository.cs index b39c3de..138877f 100644 --- a/MongoDbGenericRepository/BaseMongoDbRepository.cs +++ b/MongoDbGenericRepository/BaseMongoDbRepository.cs @@ -1087,7 +1087,7 @@ namespace MongoDbGenericRepository /// Create an Index given a field and an optional ascending / descending parameter /// we want to create them in the background as we want the db to still be available during this process /// - /// + /// The type representing a Document. /// The field we want to index /// Options for creating an index.. /// An optional partition key @@ -1095,7 +1095,24 @@ namespace MongoDbGenericRepository public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { - return await HandlePartitioned(partitionKey).Indexes + return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey); + } + + /// + /// Create an Index given a field and an optional ascending / descending parameter + /// we want to create them in the background as we want the db to still be available during this process + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index + /// Options for creating an index.. + /// An optional partition key + /// The result of the create index operation. + public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return await HandlePartitioned(partitionKey).Indexes .CreateOneAsync( new CreateIndexModel( Builders.IndexKeys.Text(field), @@ -1103,6 +1120,174 @@ namespace MongoDbGenericRepository )); } + /// + /// Creates an index on the given field in ascending order + /// + /// The type representing a Document. + /// The field we want to index + /// Options for creating an index.. + /// An optional partition key + /// The result of the create index operation. + public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + } + + /// + /// Creates an index on the given field in ascending order + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index + /// Options for creating an index.. + /// An optional partition key + /// The result of the create index operation. + public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = HandlePartitioned(partitionKey); + var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); + var indexKey = Builders.IndexKeys; + return await collection.Indexes + .CreateOneAsync( + new CreateIndexModel(indexKey.Ascending(field), createOptions)); + } + + /// + /// Creates an index on the given field in ascending order + /// + /// The type representing a Document. + /// The field we want to index + /// Options for creating an index.. + /// An optional partition key + /// + public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); + } + + /// + /// Creates an index on the given field in descending order + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index + /// Options for creating an index.. + /// An optional partition key + /// The result of the create index operation. + public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = HandlePartitioned(partitionKey); + var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); + var indexKey = Builders.IndexKeys; + return await collection.Indexes + .CreateOneAsync( + new CreateIndexModel(indexKey.Descending(field), createOptions)); + } + + /// + /// Create an Index given a field and an optional ascending / descending parameter + /// + /// The type representing a Document. + /// The field we want to index + /// Options for creating an index.. + /// An optional partition key + /// + public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); + } + + /// + /// Creates a hashed index on the given field. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index + /// Options for creating an index.. + /// An optional partition key + /// The result of the create index operation. + public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = HandlePartitioned(partitionKey); + var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); + var indexKey = Builders.IndexKeys; + return await collection.Indexes + .CreateOneAsync( + new CreateIndexModel(indexKey.Hashed(field), createOptions)); + } + + /// + /// Creates a combined text index + /// + /// The type representing a Document. + /// The fields we want to index + /// Options for creating an index.. + /// An optional partition key + /// The result of the create index operation. + public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); + } + + /// + /// Creates a combined text index + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index + /// Options for creating an index.. + /// An optional partition key + /// The result of the create index operation. + public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = HandlePartitioned(partitionKey); + var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); + var listOfDefs = new List>(); + foreach (var field in fields) + { + listOfDefs.Add(Builders.IndexKeys.Text(field)); + } + return await collection.Indexes + .CreateOneAsync(new CreateIndexModel(Builders.IndexKeys.Combine(listOfDefs), createOptions)); + } + + /// + /// Drops the index given a field name + /// + /// + /// + /// + public async Task DropIndexAsync(string fieldName) where T : BaseMongoEntity + { + var collection = GetCollection(); + await collection.Indexes.DropOneAsync(fieldName); + } + + /// + /// Drops the index given a field name + /// + /// + /// + public async Task> GetIndexesNamesAsync() where T : BaseMongoEntity + { + var collection = GetCollection(); + var indexCursor = await collection.Indexes.ListAsync(); + var indexes = await indexCursor.ToListAsync(); + var values = indexes.Select(e => e["name"].ToString()).ToList(); + return values; + } + + + #endregion Index Management + private CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions) { return new CreateIndexOptions @@ -1124,134 +1309,6 @@ namespace MongoDbGenericRepository }; } - /// - /// Creates an index on the given field in ascending order - /// - /// - /// The field we want to index - /// Options for creating an index.. - /// An optional partition key - /// - public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument - { - var collection = HandlePartitioned(partitionKey); - var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); - var indexKey = Builders.IndexKeys; - return await collection.Indexes - .CreateOneAsync( - new CreateIndexModel(indexKey.Ascending(field), createOptions)); - } - - /// - /// Creates an index on the given field in ascending order - /// - /// - /// The field we want to index - /// Options for creating an index.. - /// An optional partition key - /// - public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument - { - var collection = HandlePartitioned(partitionKey); - var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); - var indexKey = Builders.IndexKeys; - return await collection.Indexes - .CreateOneAsync( - new CreateIndexModel(indexKey.Descending(field), createOptions)); - } - - /// - /// Create an Index given a field and an optional ascending / descending parameter - /// - /// - /// - /// - /// - public async Task CreateHshedIndexAsync(Expression> field) - where TDocument : IDocument - { - var collection = HandlePartitioned(partitionKey); - var createOptions = new CreateIndexOptions - { - Background = createInBackGround - }; - var indexKey = Builders.IndexKeys; - if (ascending) - { - return await collection.Indexes - .CreateOneAsync( - new CreateIndexModel(indexKey.Ascending(field), createOptions)); - } - else - { - return await collection.Indexes - .CreateOneAsync( - new CreateIndexModel(indexKey.Descending(field), createOptions)); - } - // we want to create them in the background as we want the db to still be available during this process - var collection = GetCollection(); - var indexKey = Builders.IndexKeys; - switch (mongoCollectionIndexType) - { - case MongoCollectionIndexType.Text: - return await collection.Indexes.CreateOneAsync(indexKey.Text(field), new CreateIndexOptions { Background = true }); - case MongoCollectionIndexType.Hashed: - return await collection.Indexes.CreateOneAsync(indexKey.Hashed(field), new CreateIndexOptions { Background = true }); - default: - return await collection.Indexes.CreateOneAsync(indexKey.Hashed(field), new CreateIndexOptions { Background = true }); - } - } - - - /// - /// We are only allowed one Text index per MongoCollection, this method will combine Text indexes across multiple string fields - /// - /// - /// - /// - public async Task CreateCombinedTextIndexAsync(params Expression>[] fields) where T : BaseMongoEntity - { - var collection = GetCollection(); - var listOfDefs = new List>(); - foreach (var field in fields) - { - listOfDefs.Add(Builders.IndexKeys.Text(field)); - } - return await collection.Indexes.CreateOneAsync(Builders.IndexKeys.Combine(listOfDefs), new CreateIndexOptions - { - Background = true // we want to create them in the background as we want the db to still be available - }); - } - - /// - /// Drops the index given a field name - /// - /// - /// - /// - public async Task DropIndexAsync(string fieldName) where T : BaseMongoEntity - { - var collection = GetCollection(); - await collection.Indexes.DropOneAsync(fieldName); - } - - /// - /// Drops the index given a field name - /// - /// - /// - public async Task> GetIndexesNamesAsync() where T : BaseMongoEntity - { - var collection = GetCollection(); - var indexCursor = await collection.Indexes.ListAsync(); - var indexes = await indexCursor.ToListAsync(); - var values = indexes.Select(e => e["name"].ToString()).ToList(); - return values; - } - - - #endregion Index Management - /// /// Sets the value of the document Id if it is not set already. ///