using MongoDB.Driver; using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace MongoDbGenericRepository { public interface IBaseMongoDbIndexRepository { /// /// Create a text index on the given field. /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// 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. Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// /// Creates an index on the given field in ascending order. /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// 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. Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// /// Creates an index on the given field in descending order. /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// 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. Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// /// Creates a hashed index on the given field. /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// 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. Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// /// Creates a combined text index. /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// 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. Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// /// Drops the index given a field name /// /// The type representing a Document. /// The type of the primary key for a Document. /// The name of the index /// An optional partition key Task DropIndexAsync(string indexName, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// The type of the primary key for a Document. /// An optional partition key /// A list containing the names of the indexes on on the concerned collection. Task> GetIndexesNamesAsync(string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; } public class BaseMongoDbIndexRepository : BaseReadOnlyRepository, IBaseMongoDbIndexRepository { /// /// The constructor taking a connection string and a database name. /// /// The connection string of the MongoDb server. /// The name of the database against which you want to perform operations. protected BaseMongoDbIndexRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName) { } /// /// The contructor taking a . /// /// A mongodb context implementing protected BaseMongoDbIndexRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// /// The contructor taking a . /// /// A mongodb context implementing protected BaseMongoDbIndexRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) { } #region Index Management TKey /// public async virtual Task> GetIndexesNamesAsync(string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { var indexCursor = await HandlePartitioned(partitionKey).Indexes.ListAsync(); var indexes = await indexCursor.ToListAsync(); return indexes.Select(e => e["name"].ToString()).ToList(); } /// public async virtual 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), indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions) )); } /// public async virtual 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)); } /// public async virtual 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)); } /// public async virtual 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)); } /// public async virtual 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)); } /// public async virtual Task DropIndexAsync(string indexName, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { await HandlePartitioned(partitionKey).Indexes.DropOneAsync(indexName); } #endregion Index Management TKey } }