prep work on indexes methods
This commit is contained in:
@@ -230,7 +230,6 @@ namespace MongoDbGenericRepository
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
@@ -1082,6 +1081,177 @@ namespace MongoDbGenericRepository
|
||||
|
||||
#endregion Find And Update
|
||||
|
||||
#region Index Management
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="field">The field we want to index</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index..</param>
|
||||
/// <param name="partitionKey">An optional partition key</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
public async Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(
|
||||
Builders<TDocument>.IndexKeys.Text(field),
|
||||
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
|
||||
));
|
||||
}
|
||||
|
||||
private CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
|
||||
{
|
||||
return new CreateIndexOptions
|
||||
{
|
||||
Unique = indexCreationOptions.Unique,
|
||||
TextIndexVersion = indexCreationOptions.TextIndexVersion,
|
||||
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
|
||||
Sparse = indexCreationOptions.Sparse,
|
||||
Name = indexCreationOptions.Name,
|
||||
Min = indexCreationOptions.Min,
|
||||
Max = indexCreationOptions.Max,
|
||||
LanguageOverride = indexCreationOptions.LanguageOverride,
|
||||
ExpireAfter = indexCreationOptions.ExpireAfter,
|
||||
DefaultLanguage = indexCreationOptions.DefaultLanguage,
|
||||
BucketSize = indexCreationOptions.BucketSize,
|
||||
Bits = indexCreationOptions.Bits,
|
||||
Background = indexCreationOptions.Background,
|
||||
Version = indexCreationOptions.Version
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in ascending order
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="field">The field we want to index</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index..</param>
|
||||
/// <param name="partitionKey">An optional partition key</param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, Guid>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in ascending order
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="field">The field we want to index</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index..</param>
|
||||
/// <param name="partitionKey">An optional partition key</param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, Guid>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an Index given a field and an optional ascending / descending parameter
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="mongoCollectionIndexType"></param>
|
||||
/// <param name="fields"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> CreateHshedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, Guid>(partitionKey);
|
||||
var createOptions = new CreateIndexOptions
|
||||
{
|
||||
Background = createInBackGround
|
||||
};
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
if (ascending)
|
||||
{
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
|
||||
}
|
||||
else
|
||||
{
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(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<TDocument>();
|
||||
var indexKey = Builders<TDocument>.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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// We are only allowed one Text index per MongoCollection, this method will combine Text indexes across multiple string fields
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="fields"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> CreateCombinedTextIndexAsync<T>(params Expression<Func<T, object>>[] fields) where T : BaseMongoEntity
|
||||
{
|
||||
var collection = GetCollection<T>();
|
||||
var listOfDefs = new List<IndexKeysDefinition<T>>();
|
||||
foreach (var field in fields)
|
||||
{
|
||||
listOfDefs.Add(Builders<T>.IndexKeys.Text(field));
|
||||
}
|
||||
return await collection.Indexes.CreateOneAsync(Builders<T>.IndexKeys.Combine(listOfDefs), new CreateIndexOptions
|
||||
{
|
||||
Background = true // we want to create them in the background as we want the db to still be available
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops the index given a field name
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <returns></returns>
|
||||
public async Task DropIndexAsync<T>(string fieldName) where T : BaseMongoEntity
|
||||
{
|
||||
var collection = GetCollection<T>();
|
||||
await collection.Indexes.DropOneAsync(fieldName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops the index given a field name
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public async Task<List<string>> GetIndexesNamesAsync<T>() where T : BaseMongoEntity
|
||||
{
|
||||
var collection = GetCollection<T>();
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the document Id if it is not set already.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Options for creating an index.
|
||||
/// </summary>
|
||||
public class IndexCreationOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the index is a unique index.
|
||||
/// </summary>
|
||||
public bool? Unique { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the index version for text indexes.
|
||||
/// </summary>
|
||||
public int? TextIndexVersion { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the index version for 2dsphere indexes.
|
||||
/// </summary>
|
||||
public int? SphereIndexVersion { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the index is a sparse index.
|
||||
/// </summary>
|
||||
public bool? Sparse { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the index name.
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the min value for 2d indexes.
|
||||
/// </summary>
|
||||
public double? Min { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the max value for 2d indexes.
|
||||
/// </summary>
|
||||
public double? Max { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the language override.
|
||||
/// </summary>
|
||||
public string LanguageOverride { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets when documents expire (used with TTL indexes).
|
||||
/// </summary>
|
||||
public TimeSpan? ExpireAfter { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the default language.
|
||||
/// </summary>
|
||||
public string DefaultLanguage { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the size of a geohash bucket.
|
||||
/// </summary>
|
||||
public double? BucketSize { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the precision, in bits, used with geohash indexes.
|
||||
/// </summary>
|
||||
public int? Bits { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether to create the index in the background.
|
||||
/// </summary>
|
||||
public bool? Background { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the version of the index.
|
||||
/// </summary>
|
||||
public int? Version { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user