/// <summary> /// Verify the provided <paramref name="mongoIndex"/> is defined and ready to go. /// </summary> protected virtual void VerifyIndex(MongoDbIndex <MongoDbEventData> mongoIndex) { IndexKeysDefinitionBuilder <MongoDbEventData> indexKeysBuilder = Builders <MongoDbEventData> .IndexKeys; IndexKeysDefinition <MongoDbEventData> indexKey = null; IList <Expression <Func <MongoDbEventData, object> > > selectors = mongoIndex.Selectors.ToList(); for (int i = 0; i < selectors.Count; i++) { Expression <Func <MongoDbEventData, object> > expression = selectors[i]; if (mongoIndex.IsAcending) { if (i == 0) { indexKey = indexKeysBuilder.Ascending(expression); } else { indexKey = indexKey.Ascending(expression); } } else { if (i == 0) { indexKey = indexKeysBuilder.Descending(expression); } else { indexKey = indexKey.Descending(expression); } } } bool throwExceptions; if (!bool.TryParse(ConfigurationManager.GetSetting("Cqrs.MongoDb.EventStore.ThrowExceptionsOnIndexPreparation"), out throwExceptions)) { throwExceptions = true; } try { MongoCollection.Indexes.CreateOne ( indexKey, new CreateIndexOptions { Unique = mongoIndex.IsUnique, Name = mongoIndex.Name } ); } catch { if (throwExceptions) { throw; } } }
public static IEnumerable <CreateIndexModel <AuditEntry> > GetIndexModel(IndexKeysDefinitionBuilder <AuditEntry> builder) { yield return(new CreateIndexModel <AuditEntry>( builder.Combine( builder.Descending(entry => entry.DateValue), builder.Ascending(entry => entry.GroupId), builder.Ascending(entry => entry.DataType) ) )); }
/// <summary> /// Adds a descending index on the field to the collection /// </summary> /// <param name="collection">The collection to add the index to</param> /// <param name="field">The field to add descending index for</param> /// <param name="name">Name of the index. Can be null, then name is auto generated</param> /// <typeparam name="TDocument"></typeparam> public static void CreateDescendingIndex <TDocument>(this IMongoCollection <TDocument> collection, Expression <Func <TDocument, object> > field, string name = null) { var builder = new IndexKeysDefinitionBuilder <TDocument>(); var options = new CreateIndexOptions <TDocument> { Name = name ?? field.GetFieldName() }; collection.Indexes.CreateOne(builder.Descending(field), options); }
private static IndexKeysDefinition <T> CreateIndexDefinition <T>(IndexKeysDefinitionBuilder <T> builder, IIndexField <T> field) { switch (field.SortOrder) { case IndexSortOrder.Desc: return(builder.Descending(field.Field)); default: case IndexSortOrder.Asc: return(builder.Ascending(field.Field)); } }
protected virtual void VerifyIndexes <TEntity>(IMongoCollection <TEntity> collection) { Type entityType = typeof(TEntity); if (IndexTypesByEntityType.ContainsKey(entityType)) { foreach (object untypedIndexType in IndexTypesByEntityType[entityType]) { var mongoIndex = (MongoDbIndex <TEntity>)untypedIndexType; IndexKeysDefinitionBuilder <TEntity> indexKeysBuilder = Builders <TEntity> .IndexKeys; IndexKeysDefinition <TEntity> indexKey = null; IList <Expression <Func <TEntity, object> > > selectors = mongoIndex.Selectors.ToList(); for (int i = 0; i < selectors.Count; i++) { Expression <Func <TEntity, object> > expression = selectors[i]; if (mongoIndex.IsAcending) { if (i == 0) { indexKey = indexKeysBuilder.Ascending(expression); } else { indexKey = indexKey.Ascending(expression); } } else { if (i == 0) { indexKey = indexKeysBuilder.Descending(expression); } else { indexKey = indexKey.Descending(expression); } } } collection.Indexes.CreateOne ( indexKey, new CreateIndexOptions { Unique = mongoIndex.IsUnique, Name = mongoIndex.Name } ); } } }
private Task PrepareCollection(IMongoCollection <EventCommit> collection) { return(Task.WhenAll( collection.Indexes.CreateOneAsync(Indexes.Descending(x => x.Ordinal), new CreateIndexOptions { Unique = true }), collection.Indexes.CreateOneAsync(Indexes.Descending(x => x.AggregateId)), collection.Indexes.CreateOneAsync(Indexes.Descending(x => x.AggregateType)), collection.Indexes.CreateOneAsync(Indexes.Descending(x => x.AggregateId) .Descending(x => x.AggregateType)), collection.Indexes.CreateOneAsync( Indexes.Descending(x => x.AggregateId).Descending(x => x.AggregateType) .Descending(x => x.VersionEvents), new CreateIndexOptions { Unique = true }), collection.Indexes.CreateOneAsync( Indexes.Descending(x => x.AggregateId).Descending(x => x.AggregateType) .Descending(x => x.VersionCommit), new CreateIndexOptions { Unique = true }) )); }
private void EnsureIndexesAsDeclared(EnsureIndexAttribute attribute, string indexFieldName) { var builder = new IndexKeysDefinitionBuilder <T>(); var indexKeysDefinition = attribute.Descending ? builder.Descending(indexFieldName) : builder.Ascending(indexFieldName); var createIndexOptions = new CreateIndexModel <T>(indexKeysDefinition, new CreateIndexOptions { Unique = attribute.Unique, Sparse = attribute.Sparse }); Logger.LogDebug($"Adding index on field {indexFieldName} to collection {typeof(T).Name}"); Collection.Indexes.CreateOne(createIndexOptions); }
protected virtual void VerifyIndex(MongoDbIndex <EventData> mongoIndex) { IndexKeysDefinitionBuilder <EventData> indexKeysBuilder = Builders <EventData> .IndexKeys; IndexKeysDefinition <EventData> indexKey = null; IList <Expression <Func <EventData, object> > > selectors = mongoIndex.Selectors.ToList(); for (int i = 0; i < selectors.Count; i++) { Expression <Func <EventData, object> > expression = selectors[i]; if (mongoIndex.IsAcending) { if (i == 0) { indexKey = indexKeysBuilder.Ascending(expression); } else { indexKey = indexKey.Ascending(expression); } } else { if (i == 0) { indexKey = indexKeysBuilder.Descending(expression); } else { indexKey = indexKey.Descending(expression); } } } MongoCollection.Indexes.CreateOne ( indexKey, new CreateIndexOptions { Unique = mongoIndex.IsUnique, Name = mongoIndex.Name } ); }
/// <summary> /// Creates the timeline indices /// </summary> /// <returns>Task</returns> public async Task CreateTimelineIndices() { IndexKeysDefinitionBuilder <TimelineEntry> timelineIndexBuilder = Builders <TimelineEntry> .IndexKeys; CreateIndexModel <TimelineEntry> timelineIndex = new CreateIndexModel <TimelineEntry>(timelineIndexBuilder.Descending(x => x.Timestamp)); await _TimelineCollection.Indexes.CreateOneAsync(timelineIndex); CreateIndexModel <TimelineEntry> projectIndex = new CreateIndexModel <TimelineEntry>(timelineIndexBuilder.Ascending(x => x.ProjectId)); await _TimelineCollection.Indexes.CreateOneAsync(projectIndex); CreateIndexModel <TimelineEntry> usernameIndex = new CreateIndexModel <TimelineEntry>(timelineIndexBuilder.Ascending(x => x.Username)); await _TimelineCollection.Indexes.CreateOneAsync(usernameIndex); }
/// <summary> /// 建立索引 /// </summary> /// <param name="index">索引器</param> /// <returns></returns> protected virtual IndexKeysDefinition <T> OnEnsureIndex(IndexKeysDefinitionBuilder <T> index) { return(index.Descending(item => item.CreateTime)); }